Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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 以div内的跨度为目标,从谷歌搜索结果中获取信息_Python_Beautifulsoup - Fatal编程技术网

Python 以div内的跨度为目标,从谷歌搜索结果中获取信息

Python 以div内的跨度为目标,从谷歌搜索结果中获取信息,python,beautifulsoup,Python,Beautifulsoup,我正试图用python构建一个scraper,但我不能针对多个div中的span元素。 URL是谷歌搜索结果,因此让我们以停车场为例: 我想在结果页面中找到这个停车场的名称。(在我的屏幕截图上突出显示的元素) 我使用了我以前创建的这个函数,但问题是它会在网页上获取多个结果,并淹没我的excel。然后,当我用不需要的结果保存它时,如何在多个div中专门针对一个跨度,如果没有id 如果您需要更多信息来解决我的问题,请提前向我表示感谢!:) 尝试以下代码获取span元素。找到父元素div元素,然后

我正试图用python构建一个scraper,但我不能针对多个div中的span元素。 URL是谷歌搜索结果,因此让我们以停车场为例:

我想在结果页面中找到这个停车场的名称。(在我的屏幕截图上突出显示的元素)

我使用了我以前创建的这个函数,但问题是它会在网页上获取多个结果,并淹没我的excel。然后,当我用不需要的结果保存它时,如何在多个div中专门针对一个跨度,如果没有id


如果您需要更多信息来解决我的问题,请提前向我表示感谢!:)

尝试以下代码获取span元素。找到父元素
div
元素,然后使用
find\u next('span')

输出

AirPark Erfurt airport parking Jaritz & Büttner GbR
url_list=['url1','url2']
names=[]
hdr={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
for url in url_list:
    req = requests.get(url, headers=hdr)
    soup = BeautifulSoup(req.text, 'html.parser')
    try:
        names.append(soup.find('div', class_='SPZz6b').find_next('span').text)
    except:
        names.append("None")

print(names)

更新

AirPark Erfurt airport parking Jaritz & Büttner GbR
url_list=['url1','url2']
names=[]
hdr={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
for url in url_list:
    req = requests.get(url, headers=hdr)
    soup = BeautifulSoup(req.text, 'html.parser')
    try:
        names.append(soup.find('div', class_='SPZz6b').find_next('span').text)
    except:
        names.append("None")

print(names)

共享链接或html,否则我们无法帮助您哦,是的,对不起,我放弃了如果我没有一个url,但有一个url列表,我该怎么办?使用for循环并迭代您的url。这就是我所做的,但当我尝试用此启动它时,我返回一个错误:“str”对象在url\u list:req=Request中没有url的属性“text”(url,headers=hdr)page=urlopen(req)soup=BeautifulSoup(page,'html.parser')生成soup def getPropNames(soup):result=soup.find('div',class='SPZz6b')。find_next('span')。result中的elm文本:names.append(elm.text)这是因为每个url都不存在搜索类名吗?