如何把网站推广出,网站开发招标方案范本,长春电商网站建设哪家好,公司网站建设要注意的问题例1、爬取公众号文章中的图片。
1#xff0c;首先打开要获取公众号文章的地址 2#xff0c;按下F12#xff0c;再按Ctrl Shift C#xff0c;然后鼠标移动到图片位置#xff0c;然后观察控制台中显示图片对应的代码位置 3#xff0c;分析该位置的代码段 代码段如下…例1、爬取公众号文章中的图片。
1首先打开要获取公众号文章的地址 2按下F12再按Ctrl Shift C然后鼠标移动到图片位置然后观察控制台中显示图片对应的代码位置 3分析该位置的代码段 代码段如下 img data-s300,640 data-typepng data-srchttp://mmbiz.qpic.cn/mmbiz_png/xXrickrc6JTO9TThicnuGGR7DtzWtslaBl2kjpHsq1xSmicGGreQ5yUTK6W8JlX30aB50615I06bqib4Bk17F4nV8A/0?wx_fmtpng stylewidth: 677px !important; height: auto !important; visibility: visible !important; class data-ratio0.5602272727272727 data-w880 _width677px srchttp://mmbiz.qpic.cn/mmbiz_png/xXrickrc6JTO9TThicnuGGR7DtzWtslaBl2kjpH…50615I06bqib4Bk17F4nV8A/640?wx_fmtpngtpwebpwxfrom5wx_lazy1wx_co1 crossoriginanonymous data-fail0 这里我们观察这个代码段的格式然后编写正则表达式
pattern ‘data-type“png” data-src(.?)’
? --- 匹配位于之前的0个或1个字符--- 匹配位于之前的字符或子模块的1次或多次的出现
. --- 匹配除换行符以外的任意单个字符from re import findall
from urllib.request import urlopenurl https://mp.weixin.qq.com/s?__bizMzI4MzM2MDgyMQmid2247486249idx1sna37d079f541b194970428fb2fd7a1ed4chksmeb8aa073dcfd2965f2d48c5ae9341a7f8a1c2ae2c79a68c7d2476d8573c91e1de2e237c98534scene21#wechat_redirect #这个为要爬取公众号图片的地址
with urlopen(url) as fp:contentfp.read().decode(utf-8)pattern data-typepng data-src(.?)
#查找所有图片链接地址
result findall(pattern, content) #捕获分组
#逐个读取图片数据并写入本地文件
pathf:/test/#把图片存放到f盘下的test文件夹中
for index, item in enumerate(result):with urlopen(str(item)) as fp:with open(pathstr(index).png,wb) as fp1: fp1.write(fp.read())例2、使用scrapy框架编写爬虫程序。
首先安装scrapy打开cmd运行pip install scrapy 若出错attrs() got an unexpected keyword argument ‘eq’ 则运行pip3 install attrs19.2.0 -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com即可
运行cmd开始创建项目根据指定位置可以切换路径 创建一个项目scrapy startproject sqsq为项目名可随意 cd sq 出现这样表示scrapy框架已经搭建成功
例3、使用scrapy框架编写爬虫程序爬取天涯小说。
这里以例2为基础继续 scrapy genspider xiaoshuosq bbs.tianya.cn/post-16-1126849-1.shtml xiaoshuosq为爬虫名称 bbs.tianya.cn/post-16-1126849-1.shtml为爬虫起始位置这里是天涯小说第一页 之后打开创建的xiaoshuosq爬虫 编写如下代码
# -*- coding: utf-8 -*-
import scrapyclass XiaoshuosqSpider(scrapy.Spider):name xiaoshuosq#这里的是你创建的爬虫名称allowed_domains [http://bbs.tianya.cn/post-16-1126849-1.shtml]start_urls [http://bbs.tianya.cn/post-16-1126849-1.shtml/]def parse(self, response):content[]for i in response.xpath(//div):if i.xpath(_hostid).extract()[13357319]:for j in i.xpath(div//div):c j.xpath(text()).extract()g lambda x:x.strip(\n\r\u3000).replace(br,\n).replace(|,)c \n.join(map(g.c)).strip()content.append(c)with open(F:\result.txt,a,encondingutf8) as fp:fp.writelines(content)urlresponse.urld url[url.rindex(-)1:url.rindex(.)]u http://bbs.tianya.cn/post-16-1126849-{0}.shtmlnext_url u.format(int(d)1)try:yield scrapy.Request(urlnext_url,callbackself.parse)except:pass
保存该爬虫 然后scrapy crwal xiaoshuosq这里的xiaoshuosq是你创建的爬虫名称
例4、使用requests库爬取微信公众号“Python小屋”文章“Python使用集合实现素数筛选法”中的所有超链接。
# -*- coding: utf-8 -*-Created on Mon Jun 1 21:40:19 2020author: 78708
#使用requests库爬取微信公众号“Python小屋”文章“Python使用集合实现素数筛选法”中的所有超链接
import requests
import re
url https://mp.weixin.qq.com/s?__bizMzI4MzM2MDgyMQmid2247486531idx1sn7eeb27a03e2ee8ab4152563bb110f248chksmeb8aa719dcfd2e0f7b1731cfd8aa74114d68facf1809d7cdb0601e3d3be8fb287cfc035002c6#rd
r requests.get(url)
print(r.status_code ) #响应状态码
#print(r.text[:300] ) #查看网页源代码前300个字符
print(筛选法 in r.text )
print(r.encoding )
links re.findall(ra .*?href(.?), r.text)
#使用正则表达式查找所有超链接地址
for link in links:if link.startswith(http):print(link)from bs4 import BeautifulSoup
soup BeautifulSoup(r.content, lxml)
for link in soup.findAll(a): #使用BeautifulSoup查找超链接地址href link.get(href)if href.startswith(http): #只输出绝对地址print(href)
例5、读取并下载指定的URL的图片文件。
# -*- coding: utf-8 -*-Created on Mon Jun 1 21:39:44 2020author: 78708
#读取并下载指定的URL的图片文件。import requests
picUrl rhttps://www.python.org/static/opengraph-icon-200x200.png
r requests.get(picUrl)
print(r.status_code)
with open(G:\TIM\图片\wsq.png, wb) as fp:#G:\TIM\图片\wsq.png 为保存路径以及图片名称fp.write(r.content) #把图像数据写入本地文件