如何刮痧?


import requests
from bs4 import BeautifulSoup
url = "https://www.jab.de/tr/en/productadvancedsearch?searchTerm=&page=1"
website = requests.get(url)
html = website.content
soup = BeautifulSoup(html,"html.parser")
urunListesi = soup.find("section",{"class":"results"}).find("div",{"class":"col-item details"})
# print(urunListesi)
for urun in urunListesi:
link = urun.div.a.get("href")
print(link)
print("----------------------------n")

当我对代码进行操作时,它返回None,你能帮助我吗?

这是一个我刮比特币价格的例子!之后我会解释这一切是如何运作的。试一试!

import requests
from bs4 import BeautifulSoup
cmc = requests.get(f"https://www.google.com/search?q=what+is+the+price+of+bitcoin")
soup = BeautifulSoup(cmc.content, "html.parser")
# with open("soup.txt", "w") as f:
#     f.write(soup.prettify())
class_of_text = "BNeawe iBp4i AP7Wnd"
price = soup.find("div", attrs={'class':class_of_text}).find("div", attrs={'class':class_of_text}).text
print("Here is the price of bitcoin:")
print(price)

解释部分。

import requests
from bs4 import BeautifulSoup
cmc = requests.get("https://www.google.com/search?q=what+you+want+to+scrape+with+pluses+instead+of+spaces")
soup = BeautifulSoup(cmc.content, "html.parser")
with open("soup.txt", "w") as f:
f.write(soup.prettify())

写入一个名为soup.txt的文件。在那里将是该文件的html。查看那个文件,找出你想删除的文本。命令/控制f将帮助您找到它。然后,复制这个类。它应该是这样的:BNeawe s3v9rd AP7Wnd。让它成为一个变量。然后让一个新变量等于soup.find("div", attrs={'class':Your_First_Variable}).find("div", attrs={'class':Your_First_Variable}).text。第二个变量将包含抓取的文本。

找到soup.txt中的类后,可以删除

with open("soup.txt", "w") as f:
f.write(soup.prettify())

如果你需要进一步的澄清或帮助,请评论。

最新更新