爬取今日头条街拍美女图片
2017-08-13 14:35
435 查看
爬取今日头条美女图片需要分析Ajax请求
首先打开网址:https://www.toutiao.com/search/?keyword=%E8%A1%97%E6%8B%8D
返回的数据以及每个data展开后为下图:
图中标出的是详情页的url,然后进入详情页:
详细代码:
首先打开网址:https://www.toutiao.com/search/?keyword=%E8%A1%97%E6%8B%8D
返回的数据以及每个data展开后为下图:
图中标出的是详情页的url,然后进入详情页:
详细代码:
import json import os import re from urllib.parse import urlencode from bs4 import BeautifulSoup from requests.exceptions import RequestException import requests def get_page_index(offest, keyword): data = { 'offset': offest, 'format': 'json', 'keyword': keyword, 'autoload': 'true', 'count': '20', 'cur_tab': 1 } url = 'https://www.toutiao.com/search_content/?' + urlencode(data) try: response = requests.get(url) if response.status_code == 200: return response.text return None except RequestException: print("请求索引页出错") return None def parse_page_index(html): data = json.loads(html) if data and 'data' in data.keys(): for item in data.get('data'): yield item.get('article_url') def get_page_detail(url): try: response = requests.get(url) if response.status_code == 200: return response.text return None except RequestException: print("请求详情页出错", url) return None def parse_page_detail(html, url): soup = BeautifulSoup(html, 'lxml') title = soup.select('title')[0].get_text() # print("hah"+title) images_pattern = re.compile('gallery: (.*?),\n', re.S) # print(images_pattern) result = re.search(images_pattern, html) if result: # print("haha"+result.group(1)) data = json.loads(result.group(1)) if data and 'sub_images' in data.keys(): sub_images = data.get('sub_images') # print(sub_images) images = [item.get('url') for item in sub_images] if images: return { 'title': title, 'url': url, 'images': images } else: pass def main(): html = get_page_index(0, '街拍') for url in parse_page_index(html): html = get_page_detail(url) if html: result = parse_page_detail(html, url) if result is not None: for url in result.get('images'): print(url) pic=requests.get(url) pic_cun='F:\images\\'+str(url)[-8:-1]+'.jpg' fp = open(pic_cun, 'wb') # 以二进制写入模式新建一个文件 fp.write(pic.content) # 把图片写入文件 fp.close() if __name__ == '__main__': main()
相关文章推荐
- 今日头条街拍图片爬取
- 今日头条爬美女图片知识点1
- 爬虫实践---今日头条<街拍>图片
- 今日头条爬美女图片知识点下
- python 抓取今日头条街拍图片并下载到本地
- python3正则+bs4+requests爬取今日头条街拍图片(ajax+html)
- 一个仿网易、今日头条的图片游览器
- 一个仿网易、今日头条的图片游览器
- python 抓取头条街拍图片
- 一个仿网易、今日头条的图片游览器
- 小白学python-今日头条街拍美图详解
- python多线程爬取-今日头条的街拍数据(附源码加思路注释)
- 分析AJAX抓取今日头条街拍美图(下)
- 一个仿网易、今日头条的图片游览器
- 今日头条街拍爬取
- 一个仿网易、今日头条的图片游览器
- requests分析Ajax来爬取今日头条街拍美图
- 一个仿网易、今日头条的图片游览器
- Python爬虫实战02:分析Ajax请求并抓取今日头条街拍
- Python基于分析Ajax请求实现抓取今日头条街拍图集功能示例