【发布时间】:2017-05-13 18:14:00
【问题描述】:
我正在尝试提取此网页上评论的网址 http://uk.ign.com/games/reviews 然后在单独的选项卡中打开前 5 个
现在,我尝试了不同的选择来尝试获取正确的数据,但似乎没有返回任何内容。我似乎无法超越提取列表中每条评论的网址,更不用说在单独的标签中打开前 5 条了。
我在 Python IDE 中使用 Python 3
这是我的代码:
import webbrowser, bs4, requests, re
webPage = requests.get("http://uk.ign.com/games/reviews", headers={'User-
Agent': 'Mozilla/5.0'})
webPage.raise_for_status()
webPage = bs4.BeautifulSoup(webPage.text, "html.parser")
#Me trying different selections to try extract the right part of the page
webLinks = webPage.select(".item-title")
webLinks2 = webPage.select("h3")
webLinks3 = webPage.select("div item-title")
print(type(webLinks))
print(type(webLinks2))
print(type(webLinks3))
#I think this is where I've gone wrong. These all returning empty lists.
#What am I doing wrong?
lenLinks = min(5, len(webLinks))
for i in range(lenLinks):
webbrowser.open('http://uk.ign.com/' + webLinks[i].get('href'))
【问题讨论】:
-
运气好能找到这些链接吗?
-
我可以找到网页上的所有链接,但我无法提取我想要的链接。 webLinks = webPage.find_all('a') 给了我页面上的所有链接现在我正在尝试使用“h3”类提取“item-title”下的链接。我试过 webItems = webPage.find_all('a', {'class' : "title"}) webby = webPage.find_all(class_="h3") 这些都不起作用,也许我应该使用一些 for 循环种类?
标签: python python-3.x web web-scraping