鱼C论坛

 找回密码
 立即注册
查看: 1443|回复: 6

爬虫正则

[复制链接]
发表于 2023-11-6 21:21:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
不知道哪里错了

import requests
import re

url = 'https://book.douban.com/top250'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
}
response = requests.get(url, headers=headers)
text = response.text

# 修正正则表达式变量名为 regex
regex = '<div class="article">.*?<img src="(.*?)" width=".*?">.*?<div class="pl2">.*?<a href=".*?" onclick=".*?" title=".*?">(.*?)</a>.*?<p class="pl">(.*?)</p>.*?<span class="rating_nums">(.*?)</span>.*?<span class="pl">(.*?)</span>.*?</div>'

results = re.findall(regex, text, re.S)

# 修正缩进
for item in results:
    # 循环内的处理代码
    image = item[0]
    title = item[1]
    author = item[2].strip()
    rating = item[3]
    rating_num = item[4]
    print(image, title, author, rating, rating_num)

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-6 21:23:09 | 显示全部楼层
以下是代码的修改版:
import requestsimport reurl = 'https://book.douban.com/top250'headers = {    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"}response = requests.get(url, headers=headers)text = response.text# 修改正则表达式变量名为 regexregex = r'<div class="item">.*?<img src="(.*?)" width=".*?"/>.*?<div class="pl2">.*?<a href="(.*?)" onclick=".*?" title="(.*?)">.*?</a>.*?<p class="pl">(.*?)</p>.*?<span class="rating_nums">(.*?)</span>.*?<span class="pl">(.*?)</span>.*?</div>'results = re.findall(regex, text, re.S)# 修正缩进问题for item in results:    # 循环内的处理代码    image = item[0]    title = item[2]    author_and_publisher = item[3].strip().split('/')    author = author_and_publisher[0]    publisher = author_and_publisher[-3]    rating = item[4]    rating_num = item[5].strip('(').strip(')').strip()    print(image, title, author, publisher, rating, rating_num)
这个程序通过爬取豆瓣读书的TOP250列表,并使用正则表达式对HTML源代码进行解析,提取出每本书的信息,并将其输出。它使用了requests库发送HTTP请求,使用re库进行正则表达式匹配。

在修改代码时,我修正了正则表达式的变量名为regex;将正则表达式中的缩进根据PEP 8规范进行了调整;并在输出中加入了出版社信息的提取。

希望这个回答对你有帮助。如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-11-6 21:26:07 | 显示全部楼层
Mike_python小 发表于 2023-11-6 21:23
以下是代码的修改版:

无法输出结果啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-6 21:27:41 | 显示全部楼层
代码中存在几个问题,导致无法正确提取目标数据。

首先,正则表达式中的 <div class="article"> 部分是不正确的,因为网页源码中并没有这个标签。应该将其修改为 <li class="subject-item"> ,因为每个图书信息都是以这个标签包裹的。

其次,正则表达式中的 <div class="pl2">.*?<a href=".*?" onclick=".*?" title=".*?">(.*?)</a> 部分也是不正确的。因为网页源码中的 <div class="pl2"> 标签后面并没有 <a> 标签,而是直接是 <a> 标签的内容。所以应该将其修改为 <div class="pl2">(.*?)</div> 。

最后,正则表达式中的 <span class="pl">(.*?)</span> 部分也是不正确的。因为网页源码中的 <span class="pl"> 标签后面并没有其他标签,只有文本内容。所以应该将其修改为 <span class="pl">(.*?)</span> 。

修改后的代码如下所示:
import requests
import re

url = 'https://book.douban.com/top250'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
}
response = requests.get(url, headers=headers)
text = response.text

regex = '<li class="subject-item">.*?<img src="(.*?)" width=".*?">.*?<div class="info">.*?<div class="title">(.*?)</div>.*?<div class="pub">(.*?)</div>.*?<span class="rating_nums">(.*?)</span>.*?<span class="pl">(.*?)</span>.*?</li>'

results = re.findall(regex, text, re.S)

for item in results:
    image = item[0]
    title = item[1]
    author = item[2].strip()
    rating = item[3]
    rating_num = item[4]
    print(image, title, author, rating, rating_num)

这样修改后的代码应该可以正确提取目标数据了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-11-6 21:37:33 | 显示全部楼层
isdkz 发表于 2023-11-6 21:27
代码中存在几个问题,导致无法正确提取目标数据。

首先,正则表达式中的  部分是不正确的,因为网页源码 ...

<div class="article"> 有啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-7 21:35:57 | 显示全部楼层
isdkz 发表于 2023-11-6 21:27
代码中存在几个问题,导致无法正确提取目标数据。

首先,正则表达式中的  部分是不正确的,因为网页源码 ...

试了下,跑不出数据。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-7 21:38:54 | 显示全部楼层
你参考一下你的正则
import re

html = '''这里是包含电影top250信息的HTML代码'''

pattern = r'<img src="(.*?)" alt="(.*?)" />\s*<p class="title">(.*?)</p>\s*<p class="author">(.*?)</p>\s*<p class="rating">评分:(.*?)</p>\s*<p class="likes">点赞数:(.*?)</p>'
movies = re.findall(pattern, html, re.S)

for movie in movies:
    img_url, title, author, rating, likes = movie
    print(f'图片链接:{img_url}
标题:{title}
作者:{author}
评分:{rating}
点赞数:{likes}
')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 15:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表