Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用BeautifulSoup从Google搜索检索链接_Python_Search_Beautifulsoup_Tweepy - Fatal编程技术网

在Python中使用BeautifulSoup从Google搜索检索链接

在Python中使用BeautifulSoup从Google搜索检索链接,python,search,beautifulsoup,tweepy,Python,Search,Beautifulsoup,Tweepy,我正在用Tweepy和BeautifulSoup4构建一个Twitter机器人。我想将请求的结果保存在一个列表中,但我的脚本不再工作了(但几天前它还在工作)。我一直在看,我不明白。以下是我的功能: 导入请求 进口粗花呢 从bs4导入BeautifulSoup 导入URL库 导入操作系统 从tweepy导入StreamListener 从TwitterEngine导入TwitterEngine 从ConfigEngine导入TwitterAPIConfig 导入urllib.request 导入表

我正在用Tweepy和BeautifulSoup4构建一个Twitter机器人。我想将请求的结果保存在一个列表中,但我的脚本不再工作了(但几天前它还在工作)。我一直在看,我不明白。以下是我的功能:

导入请求
进口粗花呢
从bs4导入BeautifulSoup
导入URL库
导入操作系统
从tweepy导入StreamListener
从TwitterEngine导入TwitterEngine
从ConfigEngine导入TwitterAPIConfig
导入urllib.request
导入表情符号
随机输入
#桌面用户代理
USER_AGENT=“Mozilla/5.0(Macintosh;英特尔Mac OS X 10.14;rv:65.0)Gecko/20100101 Firefox/65.0”
#移动用户代理
MOBILE_USER_AGENT=“Mozilla/5.0(Linux;Android 7.0;SM-G930V构建/NRD90M)AppleWebKit/537.36(KHTML,如Gecko)Chrome/59.0.3071.125 MOBILE Safari/537.36”
#利安之杯
解析链接(url):
headers={“用户代理”:用户\代理}
resp=requests.get(url,headers=headers)
如果响应状态\ U代码==200:
soup=BeautifulSoup(分别为内容“html.parser”)
结果=[]
对于汤中的g,查找所有('div',class='r'):
anchors=g.find_all('a'))
如果锚定:
链接=锚[0]['href']
results.append(链接)
返回结果
在代码的其余部分,“url”参数是100%正确的。作为输出,我得到一个“无”。更准确地说,执行就在“results=[]”行之后停止(因此它不会进入for)

有什么想法吗?
提前非常感谢

谷歌似乎改变了页面上的HTML标记。尝试将搜索从
class=“r”
更改为
class=“rc”

import requests
from bs4 import BeautifulSoup


USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:65.0) Gecko/20100101 Firefox/65.0"

def parseLinks(url):
    headers = {"user-agent": USER_AGENT}
    resp = requests.get(url, headers=headers)
    if resp.status_code == 200:
        soup = BeautifulSoup(resp.content, "html.parser")
        results = []
        for g in soup.find_all('div', class_='rc'): # <-- change 'r' to 'rc'
            anchors = g.find_all('a')
            if anchors:
                link = anchors[0]['href']
                results.append(link)
        return results

url = 'https://www.google.com/search?q=tree'
print(parseLinks(url))
['https://en.wikipedia.org/wiki/Tree', 'https://simple.wikipedia.org/wiki/Tree', 'https://www.britannica.com/plant/tree', 'https://www.treepeople.org/tree-benefits', 'https://books.google.sk/books?id=yNGrqIaaYvgC&pg=PA20&lpg=PA20&dq=tree&source=bl&ots=_TP8PqSDlT&sig=ACfU3U16j9xRJgr31RraX0HlQZ0ryv9rcA&hl=sk&sa=X&ved=2ahUKEwjOq8fXyKjsAhXhAWMBHToMDw4Q6AEwG3oECAcQAg', 'https://teamtrees.org/', 'https://www.woodlandtrust.org.uk/trees-woods-and-wildlife/british-trees/a-z-of-british-trees/', 'https://artsandculture.google.com/entity/tree/m07j7r?categoryId=other']