我试图从网站上抓取数据,但是 find() 方法找不到标签。下面是我的代码:
import urllib2
from bs4 import BeautifulSoup
url='http://www.jbhifi.com.au/computers/laptop-notebook/'
req=urllib2.Request(url,headers={'User-Agent':'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36'})
webpage=urllib2.urlopen(req)
content=webpage.read()
soup=BeautifulSoup(content)
x=soup.find("div",attrs={'id':'content-two'})
print x #return None
然后,我发现了漂亮的 HTML 解析器头
head=soup.find('head')
print head # print out content within <head>tag
body=soup.find('body')
print body # Print None
我真的很困惑。我以前用过beautifulsoup 几次。这是我第一次遇到这个问题。任何人都知道如何解决这个问题?谢谢你。
问题已解决。我卸载了 beautifulsoup 4.2,然后安装了 beautifulsoup 4.3。该代码现在有效。