使用Request+正则抓取猫眼电影(常见问题)
目前使用Request+正则表达式,爬取猫眼电影top100的例子很多,就不再具体阐述过程!
完整代码github:https://github.com/connordb/Top-100
总结一下,容易出错的问题有:
1.没有加请求头,导致输出没有具体信息!
headers={
"User-Agent": "Mozilla / 5.0(Windows NT 6.1) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 67..3396.99 Safari / 537.36"
}
2.正则出问题:
这个没有好的办法,只能写一点,然后打印输出,看看是否正确输出,常见的问题:另起一行要有+号;另起一行的时候头尾要有引号;
具体要抓取的内容要用();
pattern=re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?title="(.*?)".*?star">(.*?)</p>.*?setime">(.*?)</p>'
+'.*?integer">(.*?)</i>.*?fraction">(.*?)</i>',re.S)
3.写入文件出现问题:
在写入文件的时候,当你输入f.write(content)会报错,报错内容显示只能写入字符串格式,而不是字典格式,此时解决办法是写入
f.write(json.dumps(conten))
当你成功输出数据时,发现没有换行,此时加入换行符
f.write(json.dumps(conten)+'/n')
当你成功输出数据时,发现汉字没有显示出来,此时需要把with open('maoyan.txt','a')改为
with open('maoyan.txt','a',encoding='utf-8'),
f.write(json.dumps(conten))改为f.write(json.dumps(conten,ensure_ascii=False)) 数据输出保存成功! 接下来我们比较一下加多进程的好处:
#!/usr/bin/python
# -*- coding:<utf-8> -*-
import requests
import time
from requests.exceptions import RequestException
import re
import json def get_one_page(url):
try:
headers={
"User-Agent": "Mozilla / 5.0(Windows NT 6.1) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 67..3396.99 Safari / 537.36"
}
response=requests.get(url,headers=headers)
if response.status_code==200:
return response.text
return None
except RequestException:
return None
def parse_one_page(html):
pattern=re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?title="(.*?)".*?star">(.*?)</p>.*?setime">(.*?)</p>'
+'.*?integer">(.*?)</i>.*?fraction">(.*?)</i>',re.S)
items=re.findall(pattern,html)
#print(items)
for item in items:
yield{
'index':item[0],
'name':item[1],
'actor':item[2].strip()[3:],
'date':item[3].strip()[5:],
'grade':item[4]+item[5]
}
def write_to_file(content):
with open('maoyan1.txt','a',encoding='utf-8') as f:
f.write(json.dumps(content,ensure_ascii=False)+'\n')
f.close
def main(offset):
url='http://maoyan.com/board/4?offset='+str(offset)
html=get_one_page(url)
parse_one_page(html)
for item in parse_one_page(html):
print(item)
write_to_file(item)
#print(html) if __name__ == '__main__':
start=time.time()
for i in range(10):
main(i*10)
end=time.time()
print('运行时间:',end-start)
输出结果为:运行时间: 1.7671008110046387
当加入多进程以后:
#!/usr/bin/python
# -*- coding:<utf-8> -*-
import requests
import time
from requests.exceptions import RequestException
import re
import json
from multiprocessing import Pool def get_one_page(url):
try:
headers={
"User-Agent": "Mozilla / 5.0(Windows NT 6.1) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 67..3396.99 Safari / 537.36"
}
response=requests.get(url,headers=headers)
if response.status_code==200:
return response.text
return None
except RequestException:
return None
def parse_one_page(html):
pattern=re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?title="(.*?)".*?star">(.*?)</p>.*?setime">(.*?)</p>'
+'.*?integer">(.*?)</i>.*?fraction">(.*?)</i>',re.S)
items=re.findall(pattern,html)
#print(items)
for item in items:
yield{
'index':item[0],
'name':item[1],
'actor':item[2].strip()[3:],
'date':item[3].strip()[5:],
'grade':item[4]+item[5]
}
def write_to_file(content):
with open('maoyan1.txt','a',encoding='utf-8') as f:
f.write(json.dumps(content,ensure_ascii=False)+'\n')
f.close
def main(offset):
url='http://maoyan.com/board/4?offset='+str(offset)
html=get_one_page(url)
parse_one_page(html)
for item in parse_one_page(html):
print(item)
write_to_file(item)
#print(html) if __name__ == '__main__':
start=time.time()
# for i in range(10):
# main(i*10)
pool=Pool()
pool.map(main,[i*10 for i in range(10)])
end=time.time()
print('运行时间:',end-start)
#main()
此时输出结果:运行时间: 1.0980627536773682
所以:
不加多进程运行时间: 1.7671008110046387,加多进程运行时间:1.0980627536773682
因此加了进程池以后,大大提高了代码运行速度!
使用Request+正则抓取猫眼电影(常见问题)的更多相关文章
- Python爬虫【三】利用requests和正则抓取猫眼电影网上排名前100的电影
#利用requests和正则抓取猫眼电影网上排名前100的电影 import requests from requests.exceptions import RequestException imp ...
- (python3爬虫实战-第一篇)利用requests+正则抓取猫眼电影热映口碑榜
今天是个值得纪念了日子,我终于在博客园上发表自己的第一篇博文了.作为一名刚刚开始学习python网络爬虫的爱好者,后期本人会定期发布自己学习过程中的经验与心得,希望各位技术大佬批评指正.以下是我自己做 ...
- Python爬虫之requests+正则表达式抓取猫眼电影top100以及瓜子二手网二手车信息(四)
requests+正则表达式抓取猫眼电影top100 一.首先我们先分析下网页结构 可以看到第一页的URL和第二页的URL的区别在于offset的值,第一页为0,第二页为10,以此类推. 二.< ...
- Python Spider 抓取猫眼电影TOP100
""" 抓取猫眼电影TOP100 """ import re import time import requests from bs4 im ...
- 爬虫基本库request使用—爬取猫眼电影信息
使用request库和正则表达式爬取猫眼电影信息. 1.爬取目标 猫眼电影TOP100的电影名称,时间,评分,等信息,将结果以文件存储. 2.准备工作 安装request库. 3.代码实现 impor ...
- 利用request和re抓取猫眼电影排行
import requests import re import time def get_one_page(url): headers = { 'User-Agent': 'Mozilla/5.0 ...
- python抓取猫眼电影列表
抓取地址:http://maoyan.com/board/4 分析url分页规则:http://maoyan.com/board/4?offset=0 其中offset参数值为0到90 用到的库: P ...
- 抓取猫眼电影top100的正则、bs4、pyquery、xpath实现方法
import requests import re import json import time from bs4 import BeautifulSoup from pyquery import ...
- requests+正则爬取猫眼电影前100
最近复习功课,日常码农生活. import requests from requests.exceptions import RequestException import re import jso ...
随机推荐
- python第一百六十九天,第十九周作业
FIRSTCRM 学员管理开发需求: 1.分讲师\学员\课程顾问角色, 2.学员可以属于多个班级,学员成绩按课程分别统计 3.每个班级至少包含一个或多个讲师 4.一个学员要有状态转化的过程 ,比如未报 ...
- windbg调试子进程
windbg 调试子进程 学习过程中遇到了一个从前未调试过的情景:我正在调试的进程通过CreateProcessW创建了一个子进程,我需要去了解子进程中发生的行为. 那么怎么去调试呢?OD 就有点麻烦 ...
- IE6浏览器无法打开QQ邮箱
原因:未启用TLS1.0 解决方法: 打开IE浏览器,依次打开 [Internet]→[高级],在 设置 选项卡中,勾选[使用TLS1.0],然后点击[确定]保存修改,重启浏览器即可.
- java操作elasticsearch实现query String
1.CommonTersQuery: 指定字段进行模糊查询 //commonTermsQuery @Test public void test35() throws UnknownHostExcept ...
- python数据类型练习题
一.元素分类 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中.即: { ...
- 贪心——D - Radar Installation
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- C#反射の一个泛型反射实现的网络请求框架
点击下载源码 C#反射の反射详解(点击跳转)C#反射の反射接口(点击跳转)C#反射反射泛型接口(点击跳转)C#反射の一个泛型反射实现的网络请求框架(点击跳转)
- C#事件の.net下的EventArgs和EventHandler
事件参数(EventArgs) .Net框架里边提供的一个委托EventHandler来Handle事件. 一样,搞一个场景(这个场景是书里的):买车.经销商(CarDealer)会上新车(NewCa ...
- Spring Security(十九):6. Security Namespace Configuration
6.1 Introduction Namespace configuration has been available since version 2.0 of the Spring Framewor ...
- 前向星&链式前向星
参考博文: https://blog.csdn.net/acdreamers/article/details/16902023 前向星 len[i]以i为起点的边在数组中的存储长度 head[i]以i ...