python爬虫——使用xpath爬取搜狗微信文章
2019-04-07 23:25
225 查看
版权声明:转载需注明来源 https://blog.csdn.net/weixin_44024393/article/details/89077363
缺点:爬取一定数量之后会出现验证码,导致不能继续爬取,需要更换ip才可以继续爬取,或者在浏览器重新输入验证码之后复制cookie后方能继续爬取。
import requests from fake_useragent import UserAgent from urllib.parse import urlencode from lxml import etree import re import json import csv import queue import threading # 声明一个列表,存储字典 data_list = [] # 构造请求头 ua = UserAgent() # 设置cookie,可以爬取多页,如果不设置最多只能爬取10页 headers = { 'user-agent': ua.Chrome, 'cookie': '自己设置' } # 创建多线程 class MyThread(threading.Thread): def __init__(self, q): threading.Thread.__init__(self) self.q = q def run(self) -> None: self.get_index() def get_index(self): url = self.q.get() try: # 访问改网址 resp = requests.get(url, headers=headers) # 将返回的数据转成lxml格式,之后使用xpath进行抓取 html = etree.HTML(resp.content) # 分析网页代码先抓取全部li标签 lis = html.xpath('//ul[@class="news-list"]/li') # 循环遍历li标签,抓取每章微信文章的题目,作者,发表时间,摘要,链接等 for li in lis: # 声明一个字典存储数据 data_dict = {} # 爬取文章的题目 title = ''.join(li.xpath('.//h3//text()')).strip() # 爬取文章的链接 href = li.xpath('.//h3/a/@data-share')[0] # 爬取作者名称 author = li.xpath('.//div[@class="s-p"]/a/text()')[0] # 爬取文章发表的时间 # 先爬取时间戳,抓取到的时间戳文本为:"document.write(timeConvert('1526012173'))" time_convert = li.xpath('.//div[@class="s-p"]/span/script/text()')[0] # 使用正则表达式匹配时间戳 datetime = re.findall(r'\d+', time_convert)[0] # 将爬取到的数据存储到json文件 data_dict['title'] = title data_dict['author'] = author data_dict['datetime'] = datetime data_dict['href'] = href print(data_dict) # 将字典存入列表中 data_list.append(data_dict) except Exception as e: # 如果访问超时就打印错误信息,并将该条url放入队列,防止出错的url没有爬取 self.q.put(url) print(e) def main(): # 创建队列存储url q = queue.Queue() for page in range(1, 10): # 构造参数 data = { 'query': 'python爬虫', 'type': '2', 'page': page } # 将url的参数进行编码后拼接到url url = 'https://weixin.sogou.com/weixin?' + urlencode(data) # 将拼接好的url放入队列中 q.put(url) # 如果队列不为空,就继续爬 while not q.empty(): # 创建3个线程 ts = [] for count in range(3): t = MyThread(q) ts.append(t) for t in ts: t.start() for t in ts: t.join() if __name__ == '__main__': # 启动爬虫 main() if data_list: # 将数据写入json with open('weixin_data_json.json', 'a+', encoding='utf-8') as f: # 使用json.dump方法将数据写入json文件 json.dump(data_list, f, ensure_ascii=False, indent=4) print('写入json文件完成') # 将json文件中的数据写入csv文件 # csv的表头 title = data_list[0].keys() with open('weixin_data_json.csv', 'w', encoding='utf-8', newline='') as f: writer = csv.writer(f) # 写入表头 writer.writerow(title) # 批量写入数据 for row in data_list: writer.writerow(row.values()) print('写入csv文件完成')
相关文章推荐
- python爬虫实战--------搜狗微信文章(IP代理池和用户代理池设定----scrapy)
- 【学习】06 爬虫使用代理地址爬取搜狗微信文章
- python3爬虫用代理池爬虫搜狗微信文章
- python爬虫实战(三)--------搜狗微信文章(IP代理池和用户代理池设定----scrapy)
- python3爬虫必学Xpath,快速使用lxml.etree
- python爬虫系列(七):XPath的使用
- [Python爬虫] 之十五:Selenium +phantomjs根据微信公众号抓取微信文章
- python3[爬虫实战] 使用selenium,xpath爬取京东手机(上)
- python爬虫之xpath的基本使用详解
- python爬虫利器-xpath使用
- Python使用Xpath轻松爬虫(脑残式)
- python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息,抓取政府网新闻内容
- python3[爬虫实战] 使用selenium,xpath爬取京东手机(下)
- nodejs爬虫-通过抓取搜狗微信网站获取微信文章信息
- python3[爬虫实战] 使用selenium,xpath爬取京东手机(上)
- python爬虫之xpath的使用方法
- Python爬虫(十三)_案例:使用XPath的爬虫
- windows下使用python的scrapy爬虫框架,爬取个人博客文章内容信息
- 【python爬虫】scrapy框架笔记(一):创建工程,使用scrapy shell,xpath
- 使用pyquery爬取搜狗微信文章