正则表达式出现异常
正则表达式部分出现阴影,pycharm一直显示在运行,但是就是不出现结果import requests
from requests.exceptions import RequestException
import re
headers = {
'Content-Type': 'text/html; charset=utf-8',
'Origin': r'https://maoyan.com',
'Referer': r'https://maoyan.com/board/4',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'
}
cookies ={"Cookie":"""__mta=145013643.1632827792736.1633057494521.1633061039391.33; uuid_n_v=v1; uuid=85DA7560204D11EC9B102B1A959882D0A97A7D0F881E4ACF90ED4B138FB89D1E; _lxsdk_cuid=17c2c1edc9ec8-0f7c6d795c2f3d-a7d193d-144000-17c2c1edc9ec8; _lxsdk=85DA7560204D11EC9B102B1A959882D0A97A7D0F881E4ACF90ED4B138FB89D1E; _csrf=c996b5dd3a909586fa13721ce47e20c12cb7b63b30767bb486a654d025637589; Hm_lvt_703e94591e87be68cc8da0da7cbd0be2=1632995467,1632999503,1633005709,1633053333; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; __mta=145013643.1632827792736.1632838202638.1633053344019.31; Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2=1633061039; _lxsdk_s=17c3a05eba0-622-9ad-929%7C%7C3"""}
def get_one_page(url):
try:
response = requests.get(url,headers=headers,cookies=cookies)
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>.*?src="(.*?)".*?name"><a.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i><dd>', re.S)
items = re.findall(pattern,html)
print(items)
def main():
url = "https://maoyan.com/board/4"
html = get_one_page(url)
parse_one_page(html)
if __name__ == '__main__':
main()
是不是我的正则写的有问题
我找到原因了,其实正则没有错,我这次写的稍微短了一点,运行成功了、
大家随便回复一下,我设置成最佳答案,不浪费公共资源了 好久没用过正则了,昨天还专门去看了一下,不知道是不是 findall 和分组不太搭,我是先 findall,再遍历分组的
import re
with open('wyyname.html', 'r',encoding='utf8') as f:
html = f.read()
pattern = re.compile('<dd>.*?<img data-src=".*?" alt=".*?" class="board-img" />.*?</dd>', re.S)
items = pattern.findall(html)
pattern1 = re.compile('<dd>.*?<img data-src="(.*?)" alt="(.*?)" class="board-img" />.*?</dd>', re.S)
for item in items:
# print(item)
i1 = pattern1.search(item).group(1)
i2 = pattern1.search(item).group(2)
print(i2+':'+i1)
白two 发表于 2021-10-2 11:45
好久没用过正则了,昨天还专门去看了一下,不知道是不是 findall 和分组不太搭,我是先 findall,再遍历分 ...
谢谢回复 寄安 发表于 2021-10-2 17:26
谢谢回复
你写 findall 可以用分组吗?我只有遍历 findall 的数据,然后再正则用 search 分组才行
页:
[1]