正则表达式爬取猫眼电影网

编程入门 行业动态 更新时间:2024-10-08 06:25:17

正则表达式爬取<a href=https://www.elefans.com/category/jswz/34/1763705.html style=猫眼电影网"/>

正则表达式爬取猫眼电影网

有关一些注意的问题:
(一):在爬虫的时候我经常会遇到一些爬取的内容与网页原内容不一样(缺少一些内容)
解决办法:
1.有可能是你的IP被网站识别认为是爬虫然后采取反扒机制,这个时候我们需要使用代理IP, 最好的办法就是换成你的手机热点连接。
2.可能是爬取速度过快有些内容还没被加载出来这个时候需要加入timeout=等待爬取时间
(二):爬取不到任何内容
解决办法:
1.加入header伪装成浏览器进行爬取
2.在重新执行一遍

例:

import requests
# 引入多线程模块,加快爬取速度
from multiprocessing import Pool
import json
import re
# 导入请求失败库
from requests.exceptions import RequestExceptiondef get_one_page(url):# 添加header模拟浏览器请求headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Accept-Encoding': 'gzip, deflate, br','Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2','Cache-Control': 'max-age=0','Connection': 'keep-alive','Cookie': '__mta=150384905.1582810902350.1582851008081.1582903763385.27; uuid_n_v=v1; uuid=E229D4F0596611EA8C59B911A475C6194043CEAFD70443CBB99565456091BB14; Hm_lvt_703e94591e87be68cc8da0da7cbd0be2=1582810902,1582812465,1582903748,1582903754; _lxsdk_cuid=17086e1ae9dc8-0268096f3823d-4c302879-100200-17086e1ae9ec8; _lxsdk=E229D4F0596611EA8C59B911A475C6194043CEAFD70443CBB99565456091BB14; mojo-uuid=9e2f23e3eb876013213568b9c5d73808; __mta=150384905.1582810902350.1582851008081.1582903760911.27; _csrf=779a03a5d4d24cac5f331ac8fc925d5db0c65c86d1d00e61cf48519ee02402f3; mojo-trace-id=7; mojo-session-id={"id":"ca67840304fabcc8e5bcc706d2eadd1c","time":1582903747168}; Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2=1582903763; _lxsdk_s=1708c6a661c-0b5-348-bf3%7C%7C11; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic','Host': 'maoyan','Referer': '','Upgrade-Insecure-Requests': '1'}try:response = requests.get(url, headers=headers)if response.status_code == 200:return response.textreturn "错误"except RequestException:print('请求失败')
def parse_one_page(html):# 正表达式匹配爬取的内容pattern = repile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name">''<a.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)''</p>.*?integer">(.*?)</i>.*?fraction">(\d)</i>.*?</dd>',re.S)items = re.findall(pattern,html)# 将列表定义成字典for item in items:yield{'index':item[0],'image':item[1],'title':item[2],'actor':item[3].strip()[3:],'time':item[4].strip()[5:],'score':item[5]+item[6]}# 写入文件操作
def write_to_file(content):with open('D://liang.txt','a',encoding='utf-8') as f:f.write(json.dumps(content,ensure_ascii=False)+'\n')f.close()
# 多页爬取
def main(offset):url = '='+str(offset)html = get_one_page(url)for item in parse_one_page(html):print(item)write_to_file(item)if __name__ == '__main__':# 多进程爬取pool =Pool()pool.map(main,[i*10 for i in range(10)])

更多推荐

正则表达式爬取猫眼电影网

本文发布于:2024-02-07 06:52:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754438.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:猫眼   电影网   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!