用正则表达式<dd>.?board-index.?>(.?)</i>.?data-src="(.?)".?name.?a.?>(.?)</a>.?star.?>(.?)</p>.?releasetime.?>(.?)</p>.?integer.?>(.?)</i>.?fraction.?>(.?)</i>.?</dd>爬取猫眼电影排行:爬取猫眼电影TOP100的所有信息。网址:https://www.maoyan.com/board/4
时间: 2023-07-17 15:09:00 浏览: 214
你可以使用以下代码来爬取猫眼电影TOP100的所有信息:
```python
import re
import requests
url = 'https://www.maoyan.com/board/4'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'
}
response = requests.get(url, headers=headers)
html = response.text
pattern = re.compile(r'<dd>.*?board-index.*?>(.*?)</i>.*?data-src="(.*?)".*?name.*?a.*?>(.*?)</a>.*?star.*?>(.*?)</p>.*?releasetime.*?>(.*?)</p>.*?integer.*?>(.*?)</i>.*?fraction.*?>(.*?)</i>.*?</dd>', re.S)
results = re.findall(pattern, html)
for result in results:
index = result[0]
image = result[1]
title = result[2]
actor = result[3].strip()[3:]
release_time = result[4].strip()[5:]
score = result[5] + result[6]
print(f'排名:{index}\n海报:{image}\n电影名:{title}\n主演:{actor}\n上映时间:{release_time}\n评分:{score}\n')
```
这段代码首先发送一个GET请求到指定的URL,获取网页的HTML内容。然后使用正则表达式匹配对应的信息,并进行打印输出。注意在请求中需要添加User-Agent头部信息,以模拟浏览器访问。
阅读全文