君子好逑 发表于 2020-5-10 01:08:08

正则表达式

import requests
import re
from bs4 import BeautifulSoup

html = requests.get(url='http://58921.com/alltime/2020')
html.encoding = 'utf-8'
html = html.text
#print(html)

rule = re.compile(r'<td><a title=".*?" href="/film/.*?">.*?</a></td>')
title = re.findall(rule,html)
print(title)
前面的html没问题,就是title打印出来一直是个空列表,求大佬解惑

ouyunfu 发表于 2020-5-10 13:24:05

试试这个import re
from bs4 import BeautifulSoup
import urllib.request as ur

req = ur.Request('http://58921.com/alltime/2020')
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4843.400 QQBrowser/9.7.13021.400')
page = ur.urlopen(req)
html = page.read().decode('utf-8')
print(html)
rule = re.compile(r'<a href="/film/\d+" title="(.*?)">')
title = re.findall(rule,html)
print(title)

君子好逑 发表于 2020-5-10 13:48:39

ouyunfu 发表于 2020-5-10 13:24
试试这个

好的,谢谢大佬

ouyunfu 发表于 2020-5-10 13:50:51

君子好逑 发表于 2020-5-10 13:48
好的,谢谢大佬

如果有帮助,请设为最佳答案
页: [1]
查看完整版本: 正则表达式