Python soup.findAll()为div类属性Beautifulsoup返回null

Python soup.findAll()为div类属性Beautifulsoup返回null,python,beautifulsoup,Python,Beautifulsoup,在过去的10个小时里,我一直在研究这个问题,但我仍然无法解决它。代码对某些人有效,但对我无效 主要目的是为https://www.google.com.au/webhp?num=100&gl=au&hl=en#q=site:focusonfurniture.com.au&gl=au&hl=en&start=0 这是我的代码: # -*- coding: utf-8 from bs4 import BeautifulSoup import urllib, urllib2 def google_s

在过去的10个小时里,我一直在研究这个问题,但我仍然无法解决它。代码对某些人有效,但对我无效

主要目的是为
https://www.google.com.au/webhp?num=100&gl=au&hl=en#q=site:focusonfurniture.com.au&gl=au&hl=en&start=0

这是我的代码:

# -*- coding: utf-8
from bs4 import BeautifulSoup
import urllib, urllib2

def google_scrape(query):
    address = "https://www.google.com.au/webhp?num=100&gl=au&hl=en#q=site:focusonfurniture.com.au&gl=au&hl=en&start=0".format (urllib.quote_plus(query))
    request = urllib2.Request(address, None, {'User-Agent':'Mozilla/43.0.1'})
    urlfile = urllib2.urlopen(request)
    html = urlfile.read()
    soup = BeautifulSoup(html)
    linkdictionary = {}

    for li in soup.findAll('div', attrs={'class' : 'g'}): # It never goes inside this for loop as find.All results Null

        sLink = li.find('.r a')
        print sLink['href']

    return linkdictionary

if __name__ == '__main__':
    links = google_scrape('beautifulsoup')
    print links
因此,我得到了
{}
。代码
soup.findAll('div',attrs={'class':'g'})
返回null,因此,我无法获取任何结果

我正在使用BS4和Python 2.7。请帮助我了解代码工作不正常的原因。任何帮助都将不胜感激

此外,如果有人能够深入了解为什么相同的代码适用于某些人而不适用于其他人,那就太好了?(上次也发生在我身上)。
谢谢。

这是一个你可以做的例子。 您需要selenium和phantomjs(模拟浏览器)


这是一个你可以做的例子。 您需要selenium和phantomjs(模拟浏览器)


好的,我马上看到的一个问题是,您试图使用
.format()
将查询放入
地址的字符串中,但是字符串中没有占位符来告诉Python将查询放在哪里。@kindall甚至删除它都不起作用。您在计算机上运行过相同的代码吗?它能工作吗?如果您使用内部API(或使用selenium)会更好,这可能会有所帮助@wu4m4n感谢您的回复。我会调查的。看起来有点复杂,因为我以前从未使用过API。你能解释一下为什么python代码不能刮取数据吗?这是因为Google的一些限制吗?好吧,我马上看到的一个问题是,您试图使用
.format()
将查询放入
地址
字符串中,但字符串中没有占位符来告诉Python将查询放在哪里。@kindall甚至删除它都不起作用。您在计算机上运行过相同的代码吗?它能工作吗?如果您使用内部API(或使用selenium)会更好,这可能会有所帮助@wu4m4n感谢您的回复。我会调查的。看起来有点复杂,因为我以前从未使用过API。你能解释一下为什么python代码不能刮取数据吗?这是因为谷歌的一些限制吗?非常感谢你的回复。我正在研究一些关于正确使用Selenium的错误。但我希望它能奏效。让我们看看。非常感谢您的回复。我正在研究一些关于正确使用Selenium的错误。但我希望它能奏效。让我们看看。
import selenium.webdriver
from pprint import pprint
import re 

url = 'https://www.google.com.au/webhp?num=100&gl=au&hl=en#q=site:focusonfurniture.com.au&gl=au&hl=en&start=0'
driver = selenium.webdriver.PhantomJS()
driver.get(url)
html =  driver.page_source


regex = r"<cite>(https:\/\/www\.focusonfurniture\.com\.au\/[\/A-Z]+)<\/cite>"

result = re.findall(re.compile(regex, re.IGNORECASE | re.MULTILINE),html)
for url in result:
    print url

driver.quit()
https://www.focusonfurniture.com.au/delivery/
https://www.focusonfurniture.com.au/terms/
https://www.focusonfurniture.com.au/disclaimer/
https://www.focusonfurniture.com.au/dining/
https://www.focusonfurniture.com.au/bedroom/
https://www.focusonfurniture.com.au/catalogue/
https://www.focusonfurniture.com.au/mattresses/
https://www.focusonfurniture.com.au/clearance/
https://www.focusonfurniture.com.au/careers/