爬取艺恩电影排行榜用re爬取六页信息
时间: 2024-05-17 20:14:30 浏览: 17
好的,我可以为您提供一个示例代码来爬取艺恩电影排行榜的六页信息。请先确保您已经安装了re模块。
```python
import requests
import re
for page in range(1, 7):
url = f'https://www.endata.com.cn/BoxOffice/BO/Year/total?year=2021&pIndex={page}'
response = requests.get(url)
html = response.content.decode('utf-8')
pattern = r'<tr class="even|odd">.*?<td class="titleColumn">.*?<a.*?>(.*?)</a>.*?</td>.*?<td class="ratingColumn imdbRating">.*?<strong>(.*?)</strong>'
results = re.findall(pattern, html, re.S)
for result in results:
print(result[0], result[1])
```
这段代码会爬取2021年的电影排行榜前六页信息。每页包含50部电影,因此总共会输出300部电影的名称和评分。注意,为了避免被反爬虫机制封锁,建议加入适当的延时等反爬虫措施。此外,由于使用正则表达式匹配HTML代码,代码可读性较差,建议使用其他库如BeautifulSoup或lxml。
相关问题
爬取艺恩电影排行榜用bs4爬取六页信息
好的,我可以为您提供一个示例代码来爬取艺恩电影排行榜的六页信息。请先确保您已经安装了bs4模块。
```python
import requests
from bs4 import BeautifulSoup
for page in range(1, 7):
url = f'https://www.endata.com.cn/BoxOffice/BO/Year/total?year=2021&pIndex={page}'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find('table', class_='tabBox')
rows = table.find_all('tr')
for row in rows[1:]:
cols = row.find_all('td')
name = cols[0].find('a').text
score = cols[1].find_all('span')[-1].text
print(name, score)
```
这段代码会爬取2021年的电影排行榜前六页信息。每页包含50部电影,因此总共会输出300部电影的名称和评分。注意,为了避免被反爬虫机制封锁,建议加入适当的延时等反爬虫措施。
爬取艺恩电影排行榜用lxml爬取四页信息
好的,我可以为您提供一个示例代码来爬取艺恩电影排行榜的四页信息。请先确保您已经安装了lxml模块。
```python
import requests
from lxml import etree
for page in range(1, 5):
url = f'https://www.endata.com.cn/BoxOffice/BO/Year/total?year=2021&pIndex={page}'
response = requests.get(url)
html = response.content.decode('utf-8')
selector = etree.HTML(html)
movie_names = selector.xpath('//td[@class="titleColumn"]/a/text()')
movie_scores = selector.xpath('//td[@class="ratingColumn imdbRating"]/strong/text()')
for name, score in zip(movie_names, movie_scores):
print(name, score)
```
这段代码会爬取2021年的电影排行榜前四页信息。每页包含50部电影,因此总共会输出200部电影的名称和评分。注意,为了避免被反爬虫机制封锁,建议加入适当的延时等反爬虫措施。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)