爬取豆瓣电影top250,获取250部电影片名,排名,评分,链接,显示在屏幕上,使用request和xpath
时间: 2023-10-24 15:06:06 浏览: 324
好的,我来给你写一个爬取豆瓣电影top250的代码:
```python
import requests
from lxml import etree
url = 'https://movie.douban.com/top250'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
html = etree.HTML(response.text)
for i in range(25):
xpath_rank = f'//div[@class="item"]/div[@class="pic"]/em[text()="{i+1}"]'
xpath_title = f'//div[@class="item"][{i+1}]/div[@class="info"]/div[@class="hd"]/a/span[@class="title"][1]'
xpath_rating = f'//div[@class="item"][{i+1}]/div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[@class="rating_num"][1]'
xpath_link = f'//div[@class="item"][{i+1}]/div[@class="info"]/div[@class="hd"]/a/@href'
rank = html.xpath(xpath_rank)[0].text.strip()
title = html.xpath(xpath_title)[0].text.strip()
rating = html.xpath(xpath_rating)[0].text.strip()
link = html.xpath(xpath_link)[0]
print(f'{rank}. {title} - {rating} ({link})')
```
运行后,你就可以在屏幕上看到豆瓣电影top250的排名、片名、评分和链接了。
阅读全文