Python3之利用requests和BeautifulSoup抓取部分信息
2017-09-03 14:49
716 查看
import requests import os from bs4 import BeautifulSoup imgPath = r'D:\Users\Quincy_C\PycharmProjects\S6\bs模块\汽车图片' response = requests.get(url='http://www.autohome.com.cn/news/') response.encoding = response.apparent_encoding bs = BeautifulSoup(response.text, features='html.parser') bs_obj = bs.find(id="auto-channel-lazyload-article") li_list = bs_obj.find_all('li') for i in li_list: a = i.find('a') if a: txt = a.find('h3').text print(a.find('img').attrs.get('src')) # requests.get('url').content返回的是字节 imgContent = requests.get(a.find('img').attrs.get('src')).content import uuid if not os.path.isdir(imgPath): os.mkdir(imgPath) else: imgUrl = str(uuid.uuid4()) + '.jpg' with open(os.path.join(imgPath, imgUrl), 'wb') as f: f.write(imgContent)
如果要讲图片存放在指定的文件夹,可以这样:
with open(os.path.join(imgPath, imgUrl), 'wb') as f: f.write(imgContent)
或者:
os.chdir(imgPath)
都可以的,之前搞过,忘记了。记录一下!
总结一下:
requests
requests.get(‘url’,headers=headers)发送一个请求response.encoding = response.apparent_encoding指定编码
requests.get(‘url’).text获取网页内容
requests.get(‘url’).content获取图片的字节
BeautifulSoup
bs = BeautifulSoup(requests.get(‘url’).text,features=’html.parser’)bs.find(‘div’,id=”)
bs.find_all(‘div’,id=”)
bs.find_all(‘div’,class=”)
a.attrs获取一个字典
a.ttrs.get(”)获取具体的内容
相关文章推荐
- 利用python3 requests和BeautifulSoup4抓取百度贴吧研究生录取通知书并展示
- python urllib BeautifulSoup抓取去哪儿网站景点部分信息
- [python和大数据-1]利用爬虫登录知乎进行BFS搜索抓取用户信息本地mysql分析【PART1】
- 我的第一个爬虫程序:利用Python抓取网页上的信息
- 用Python Requests抓取知乎用户信息
- python 3利用BeautifulSoup抓取div标签的方法示例
- python: 利用requests爬去51job职位信息,并写入CSV
- 用Python Requests抓取知乎用户信息(二)
- python 爬虫利用webdriver 跳过登陆 进行小规模抓取信息
- Python 利用urllib2 lxml 抓取网页信息
- [Python爬虫] 之二十六:Selenium +phantomjs 利用 pyquery抓取智能电视网站图片信息
- Python利用selenium模拟浏览器抓取异步加载等难爬页面信息
- 利用python爬虫抓取OJ上做题信息(终结版)
- 利用python爬虫抓取OJ上做题信息(扩展版)
- [python]利用BeautifulSoup进行简单图片抓取
- python爬虫抓取豆瓣所有恐怖片信息(利用多线程和构建免费ip代理池)
- 利用Python的requests模块爬取当当网的书籍信息(待续)
- python基础学习——利用requests与re来动态爬取淘宝网商品信息
- python爬虫之利用requests爬取墨迹天气
- Python爬虫实战---抓取图书馆借阅信息