【问题标题】:From the same website I can scrape a table with beautifulsoup from one page category but not the other although the html are the same从同一个网站,我可以从一个页面类别中刮一张带有美丽汤的表格,但不能从另一个页面类别中刮取一张桌子,尽管 html 是相同的
【发布时间】:2021-07-08 13:43:43
【问题描述】:

我可以轻松地从这个网址中抓取表格:

url = "https://www4.yggtorrent.li/engine/search?name=&description=&file=&uploader=&category=2145&sub_category=all&do=search&page="

使用以下代码:

r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
table = soup.find('table', attrs={'class':'table'})
print(table)

但不是来自这个(return None)虽然它来自同一个网站,但它只是另一个页面类别:
!!!下面的链接会将您转到包含色情内容链接但不显示的页面!!!

url = "https://www4.yggtorrent.li/engine/search?name=&description=&file=&uploader=&category=2188&sub_category=all&do=search&page="

我还尝试了以下变体:

r.text => r.content
html.parser => lxml, html5lib

我比较了这两个 html 页面:https://www.textcompare.org/html/ 但找不到可能使其返回 None 的差异。

【问题讨论】:

  • 你是怎么找到category=2188的?
  • soup.find("table",class_="table") 给了我正确的结果!
  • @Kshitiz 我刚刚尝试过,但它对我不起作用:(是什么让你想尝试“class_”而不是“class”?因为在 html 中它清楚地写成“class” ”。您是否将“r.text”或“r.content”与“html.parser”或“lxml”或“html5lib”一起使用?您使用的是哪个浏览器?试图了解为什么它对您有用,而不是对我有用!
  • @BhavyaParikh 通过选择网站上的“xxx”类别。

标签: html python-3.x web-scraping beautifulsoup python-requests


【解决方案1】:

这是我尝试过的完整代码,对我有用。

import requests
from bs4 import BeautifulSoup

page=requests.get("https://www4.yggtorrent.li/engine/search?name=&description=&file=&uploader=&category=2145&sub_category=all&do=search&page")
soup=BeautifulSoup(page.content,"lxml")

print(soup.find("table",class_="table"))

这给了我你想要做的确切结果。

【讨论】:

  • 你没读懂我。这是提供的第二个链接(url)是有问题的。 “类别=2188”不是“类别=2145”!这个:“www4.yggtorrent.li/engine/…
  • 你难道没有看到这根本没有给出任何结果。那你会在没有结果的情况下进行抓取吗?
  • 我不懂法语,但是在将“Aucun résultat!Essayez d'élargir votre recherche...”翻译成英文时,我收到“No result! Try to expand your search ...”和我认为这很简单。
猜你喜欢
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
  • 2022-07-11
  • 1970-01-01
  • 2018-02-04
  • 2019-03-13
相关资源
最近更新 更多