一、安装bs4(beautifulsoup4简称)
bs4与scrapy的优点就在于对解析的支持,毕竟爬虫后续解析才是最重要的,bs4是python的第三方包;
安装:sudo pip install bs4 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com;
如果提示如下:
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
则执行sudo pip install --upgrade pip;
bs安装成功后提示:Successfully installed beautifulsoup4-4.6.1 bs4-0.0.1;
二、代码如下:
#coding:utf-8 import urllib2 from bs4 import BeautifulSoup resp = urllib2.urlopen('https://www.taobao.com/', timeout=3) soup = BeautifulSoup(resp, 'lxml') tagsul = soup.find_all('a', attrs={"href":"https://3c.tmall.com"})[0].get_text() print tagsul tagsul1 =