python爬取豆瓣读书top50(正则表达式)
时间: 2023-12-20 18:32:10 浏览: 216
以下是使用正则表达式爬取豆瓣读书Top50的示例代码:
```python
import requests
import re
# 发送请求获取网页内容
url = 'https://book.douban.com/top250'
response = requests.get(url)
content = response.text
# 使用正则表达式提取书籍信息
pattern = r'<div class="pl2">.*?<a href="(.*?)" title="(.*?)".*?</div>'
results = re.findall(pattern, content, re.S)
# 打印书籍信息
for result in results:
book_url = result[0]
book_title = result[1]
print("书籍链接:", book_url)
print("书籍标题:", book_title)
print("--")
```
相关问题
python爬虫爬取豆瓣电影top250使用正则表达式
以下是使用正则表达式爬取豆瓣电影top250的Python爬虫示例代码:
```python
import re
import requests
from bs4 import BeautifulSoup
def get_movies():
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'}
movie_list = []
for i in range(0, 10):
link = 'https://movie.douban.com/top250?start=' + str(i * 25)
r = requests.get(link, headers=headers, timeout=10)
soup = BeautifulSoup(r.text, "html.parser")
div_list = soup.find_all('div', class_='hd')
for each in div_list:
movie = each.a.span.text.strip()
movie_list.append(movie)
return movie_list
def main():
movies = get_movies()
for movie in movies:
print(movie)
if __name__ == '__main__':
main()
```
用python的requests和xpath和正则表达式爬取豆瓣电影top250详情页的代码
以下是用Python的requests和xpath和正则表达式爬取豆瓣电影top250详情页的代码:
```python
import requests
from lxml import etree
import re
def get_movie_details(url):
# 发送请求获取页面内容
response = requests.get(url)
html = response.content.decode('utf-8')
# 解析页面内容
selector = etree.HTML(html)
# 获取电影名称
movie_name = selector.xpath('//h1/span/text()')[0]
# 获取导演和主演信息
director_and_cast = selector.xpath('//div[@id="info"]/span[1]/span[@class="attrs"]/a/text()')
director = director_and_cast[0]
cast = director_and_cast[1:]
# 获取上映年份、地区、类型
year_country_type = selector.xpath('//div[@id="info"]/text()')
year = re.findall('\d{4}', year_country_type[1])[0]
country = year_country_type[2].strip().split('/')[0]
type = year_country_type[2].strip().split('/')[-1]
# 获取评分和评价人数
rating = selector.xpath('//strong[@class="ll rating_num"]/text()')[0]
rating_num = selector.xpath('//div[@class="rating_sum"]/a/span/text()')[0]
# 获取电影简介
summary = selector.xpath('//div[@class="indent"]/span[@class="all hidden"]/text()')[0]
# 构造电影信息字典
movie_info = {
'名称': movie_name,
'导演': director,
'主演': cast,
'年份': year,
'地区': country,
'类型': type,
'评分': rating,
'评价人数': rating_num,
'简介': summary.strip(),
}
return movie_info
if __name__ == '__main__':
urls = ['https://movie.douban.com/top250?start={}'.format(i) for i in range(0, 250, 25)]
for url in urls:
response = requests.get(url)
html = response.content.decode('utf-8')
selector = etree.HTML(html)
# 获取电影详情页链接
movie_links = selector.xpath('//div[@class="hd"]/a/@href')
for link in movie_links:
movie_info = get_movie_details(link)
print(movie_info)
```
这段代码会爬取豆瓣电影top250列表页面中每部电影的详情页,并从详情页中提取电影名称、导演和主演、上映年份、地区、类型、评分、评价人数、电影简介等信息,并打印出来。
阅读全文