python 版本 3.6.5,代码如下:
import requests
import re
content = requests.get('http://book.douban.com/').text
pattern = re.compile('<li.?href="(.?)".?title="(.?)".*?', re.S)
results = re.findall(pattern, content)
print(results)
代码在 results = re.findall(pattern, content)这里卡住了,如果将
pattern = re.compile('<li.?href="(.?)".?title="(.?)".*?', re.S)
去掉一个()
pattern = re.compile('<li.?title="(.?)".*?', re.S)
就能正确的运行,请问我是哪里出错了?希望大家指教
1
summerwar Oct 21, 2018
正则不对,?表示重复零次或一次,网址和标题哪有那么短
|
2
PulpFunction Oct 21, 2018 写代码不能试着写啊…
1 解析网页直接上正则不太好,2requests 不加 head 容易被封 非要上正则的话,你把 findall 参数搞混错了… 建议再看文档: http://www.runoob.com/python/python-reg-expressions.html 还可以了解一下 Beautifulsoup 等等 |
3
SpiderXiantang Oct 21, 2018
了解下 xpath 不过正则还是得学的
|
4
GreatTony Oct 21, 2018
html 用正则解析效率很低和出错率挺高的,用这个库吧: https://github.com/kennethreitz/requests-html,requests 的作者的另一库,非常好用
|
5
frostming Oct 22, 2018
学正则的时候验证一下表达式
http://tool.oschina.net/regex |
6
wersonliu9527 Oct 22, 2018
不想正则搞晕,直接用谷歌浏览器的 copy xpath 功能加上 xpath helper 插件吧
|
7
canwushuang Oct 23, 2018
问题在于问号 “?” 问好表示非贪婪
|