使用Python进行基本的WebScrap(漂亮的汤和请求)

使用Python进行基本的WebScrap(漂亮的汤和请求),python,web-scraping,beautifulsoup,python-requests,Python,Web Scraping,Beautifulsoup,Python Requests,因此,我一直在阅读艾尔·斯维加特的在线Python教程,让无聊的东西自动化,而我刚刚进入了网络垃圾部分。下面是我的代码,其中描述了程序应该执行的操作: #! python3 # lucky.py - A small program that allows you to get search keywords from # command line arguments, retrieve the search results page, and open # a new browser tab f

因此,我一直在阅读艾尔·斯维加特的在线Python教程,让无聊的东西自动化,而我刚刚进入了网络垃圾部分。下面是我的代码,其中描述了程序应该执行的操作:

#! python3
# lucky.py - A small program that allows you to get search keywords from
# command line arguments, retrieve the search results page, and open
# a new browser tab for each result

# Steps:
# 1. Read the command line arguments from sys.argv
# 2. Fetch the search result page with the requests module
# 3. Find the links to each search result
# 4. Call the webbrowser.open() function to open the web browser

import sys, requests, bs4, webbrowser

# 1. Read the command line arguments from sys.argv

print('Googling...')

if len(sys.argv) > 1:
    search = ' '.join(sys.argv[1:])

url = "https://www.google.com/#q="

for i in range(len(search.split())):
    url += search.split()[i] + "+"

# 2. Fetch the search result page with the requests module

page = requests.get(url)

# 3. Find the links to each search result

soup = bs4.BeautifulSoup(page.text, 'lxml')
linkElems = soup.select('.r a')

# 4. Call the webbrowser.open() function to open the web browser

numOpen = min(5, len(linkElems))
for i in range(numOpen):
    webbrowser.open("http://google.com" + linkElems[i].get('href'))
所以这里的问题是,当我检查linkelem的长度时,它是0,这意味着soup.select“.ra”命令无法聚合class=r中元素下定义的内容。在使用开发人员工具时,可以看到,class=r是一个仅用于谷歌搜索结果的类。因此,在我的浏览器中不会打开搜索结果的网页


我认为这个问题要么与HTML解析器工作不正常有关,要么与Google改变HTML代码的工作方式有关。对此问题的任何见解都将不胜感激

linkElems=soup.find_all'a',href=True这将返回所有相关标记,您可以处理该列表以决定保留什么和不保留什么

谷歌似乎发现你是一个机器人,而不是一个拥有cookie和Javascript的真正网络浏览器。他们似乎试图用新的结果来做的,仍然是让网络爬虫跟踪他们提供的链接并给它们加上前缀,这样当你转到那个URL时,他们仍然可以跟踪你的移动

您还可以尝试在提供的链接中查找模式。例如,当您搜索“linux”时,它将返回以下内容:

/url?q=https://en.wikipedia.org/wiki/Linux&sa=U&ved=9775308e-206b-11e8-b45f-fb72cae612a8&usg=9775308e-206b-11e8-b45f-fb72cae612a8
/url?q=https://www.linux.org/&sa=U&ved=9775308e-206b-11e8-b45f-fb72cae612a8&usg=9775308e-206b-11e8-b45f-fb72cae612a8
/url?q=https://www.linux.com/what-is-linux&sa=U&ved=9775308e-206b-11e8-b45f-fb72cae612a8&usg=d50ea51a-206b-11e8-9432-2bee635f8337
/url?q=https://www.ubuntu.com/&sa=U&ved=9775308e-206b-11e8-b45f-fb72cae612a8&usg=dab9f6a4-206b-11e8-a999-3fc9d4576425
/search?q=linux&ie=UTF-8&prmd=ivns&source=univ&tbm=nws&tbo=u&sa=X&ved=9775308e-206b-11e8-b45f-fb72cae612a8
您可以使用正则表达式获取“/url?q=”和“&sa=U&ved=”之间的部分,因为这可能是您想要的url。当然,这与它返回的第五个结果不符,因为这是谷歌网站的特殊功能。同样,在返回的每个URL的前面加上图钉可能是最安全的做法


大多数搜索引擎甚至duckduckgo.com都在尝试跟踪搜索结果和点击。如果你试图避免它,他们会在适当的位置设置检测代码来阻止你。你可能遇到过这样的情况,谷歌告诉你,他们已经从你的IP检测到大量搜索,你必须通过验证码测试才能继续。

那么你是在寻找锚元素?很可能谷歌已经改变了他们提供的内容,所以你可能再也找不到你想要的东西了,至少是这样。您需要查看它们的源代码,查看哪个标记包含您想要的信息,然后提取它。@cᴏʟᴅsᴘᴇᴇᴅ 没错。有趣的是,在检查源代码后,谷歌似乎仍然使用class=r作为搜索结果,并在相应链接下使用锚元素。我会更深入地了解来源,看看是否还有另一个主要的潜在问题。谢谢你的评论!很可能是通过JS加载的。。。您可能需要看看phantomjs或selenium。祝你好运为什么不使用url=https://www.google.com/search?q= ?虽然这会返回大量链接,但不幸的是,它们都是指向谷歌其他部分的链接,如图像、视频、设置等。。。我打印出了结果列表,没有一个href值显示为搜索结果URL。无论如何,谢谢你的回答!