调用不了group函数
代码补全出问题了,你直接写进去是可以跑通的
有帮助请采纳
第17行 修改
# 开始匹配
result = re.findall(obj, page_content)
这样就可以了!
如果你想用group函数, 那么第17行应为:
# 开始匹配
result = obj.match(page_content)
result.group()
请采纳
# 拿到页面源代码
# 通过re来提取想要的有效信息
import requests
import re
url = 'https://movie.douban.com/chart'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.73'
}
resp = requests.get(url, headers=headers)
page_content = resp.text
# 解析数据
obj = re.compile(
r'<div class="">.*?"top">.*?<a class="nbg".*?title="(?P<name>.*?)">', re.S)
# 开始匹配
result = obj.match(page_content)
result.group('name')
for it in result:
print(it.)