我正在尝试提取这个网页上的评论网址 http://uk.ign.com/games/reviews 然后在单独的选项卡中打开前5个
现在,我尝试了不同的选择来获取正确的数据,但似乎没有任何结果。我似乎只能提取列表中每个评论的url,更不用说在单独的选项卡中打开前5个了。你知道吗
我在pythonide中使用python3
这是我的密码:
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'))
使用bs4、BeautifulSoup和它返回的
soup
对象(作为webPage
),可以调用:find_all
返回一个基于标题的元素列表(在您的例子中是a。这些是HTML元素;要获得链接,您需要更进一步。您可以像访问dict一样访问HTML元素的属性(在本例中,您需要href):详见BeautifulSoup getting href。当然,还有docs
pspython通常使用snake\u case而不是CamelCase来编写:)
相关问题 更多 >
编程相关推荐