How to fetch specific data from same class div using Beautifulsoup

2024/11/15 17:29:24

I have a link : https://www.cagematch.net/?id=2&nr=448&gimmick=Adam+Pearce

In this link there data in divs with same class name. But I want to fetch specifi div. Like I want to fetch current gimmik then age and brand nothing else. I tried this code :

url = "http://www.cagematch.net/?id=8&nr=1&page=15"
headers = {"Accept-Encoding": "deflate"}
soup = BeautifulSoup(requests.get(url, headers=headers).content, "html.parser")links = ["https://www.cagematch.net/" + a["href"] for a in soup.select(".TCol a")
]
list = []
for u in links:soup = BeautifulSoup(requests.get(u, headers=headers).content, "html.parser")# print(soup.h1.text)with open("wwe/maw.csv", 'a', encoding="utf-8", newline="") as f:wrt = writer(f)for info in soup.select_all(".InformationBoxRow"):# header1 = info.select_one(".InformationBoxTitle").text# header2 = info.select_one(".InformationBoxContents").text# table = [header1, header2, "/"]header1 = info.select("div", class_="InformationBoxContents").textprint(header1)

With this I am getting all the data from that page. But I want to fetch only some of them. How can I get that. Is there any easy way to do it?

Answer

Try:

import requests
import pandas as pd
from bs4 import BeautifulSoupheaders = {"Accept-Encoding": "deflate"}def get_info(url):soup = BeautifulSoup(requests.get(url, headers=headers).content, "html.parser")title = soup.h1.text.strip()gimmick = soup.select_one('.InformationBoxTitle:-soup-contains("Current gimmick") + div').text.strip()age = soup.select_one('.InformationBoxTitle:-soup-contains("Age") + div').text.strip()return {"Name": title, "Gimmick": gimmick, "Age": age}data = []
urls = ["https://www.cagematch.net/?id=2&nr=448&gimmick=Adam+Pearce"]
for url in urls:data.append(get_info(url))df = pd.DataFrame(data)
print(df)

Prints:

          Name      Gimmick       Age
0  Adam Pearce  Adam Pearce  44 years
https://en.xdnf.cn/q/119382.html

Related Q&A

Python Matplotlib Box plot

This is my dataframe:{Parameter: {0: A, 1: A, 2: A, 3: A, 4: A, 5: A, 6: A, 7: A},Site: {0: S1,1: S2,2: S1,3: S2,4: S1,5: S2,6: S1,7: S2},Value: {0: 2.3399999999999999,1: 2.6699999999999999,2: 2.560000…

How to send turtle to random position?

I have been trying to use goto() to send turtles to a random position but I get an error when running the program.I am lost on how else to do this and not sure of other ways. My current code is:t1.shap…

How to scrape all p-tag and its corresponding h2-tag with selenium?

I want to get title and content of article: example web :https://facts.net/best-survival-movies/ I want to append all p in h2[tcontent-title]and the result expected is: title=[title1, title2, title3]co…

Tkinter: Window not showing image

I am new to GUI programming and recently started working with tKinter.My problem is that the program wont show my image, Im suspecing that it is my code that is wrong, however, I would like somone to e…

print dictionary minus two elements

Python 3.6All debug output is from PyCharm 2017.1.2I have a program that gets to this portion of the code:if len(errdict) == 21:for k, v in errdict.items():if k == packets output or bytes:continueprint…

Write CSV file using Python with the help of a csv dictionary / nested csv dictionary

I am having a csv file and i want to write it to another csv file. Its a bit complicated than it seems. Hoping someone to correct my code and rewrite it, so that i can get the desired csvfile. I am usi…

saving data to txt file using python

I am new in python, and I really need some help. I am doing this memory game where I need to save user, game score and time into a text file using python. I have tried several ways to do it, but nothin…

How can I create bounding boxes/contour around the outer object only - Python OpenCV

So Ive been trying to make bounding boxes around a couple of fruits that I made in paint. Im a total beginner to opencv so I watched a couple tutorials and the code that I typed made, makes contours ar…

resuming download file ftp python3.*

There is a file (1-7Gb) that you need to pick up. The network periodically falls, so it is necessary to implement the method of resume. For example, in 1 communication session downloaded 20% the networ…

printing files based on character

I have a directory(data) that contain thousand of files.Each time I want to select three files that are just differ by only one characterAB[C,D,E] and want to perform some computation on the selected t…