|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 17623095765 于 2021-3-25 15:41 编辑
- import requests
- import bs4
- import re
- header={'ser-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
- wanzhi='http://top.baidu.com/buzz.php?p=book'
- a=requests.get(wanzhi,headers=header)
- b=bs4.BeautifulSoup(a.text,'html.parser')
- bb=re.findall(r'<a class=".*?">(..)</a>',b)
- print(bb)
复制代码
return _compile(pattern, flags).findall(string)
TypeError: expected string or bytes-like object
- type(b)
- <class 'bs4.BeautifulSoup'>
复制代码
如果直接用str(b) 会把数据变少
- >>> b
- Sqteezed text (1516 lines).
- >>> str(b)
- Sqteezed text (756 lines).
复制代码
本帖最后由 suchocolate 于 2021-3-25 18:20 编辑
正则,bs,xpath算是并列关系,用了正则就不用bs了,bs是已经对html文本做了结构话处理,就不能直接在用正则了。 - import requests
- import re
- header = {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64)'}
- url = 'http://top.baidu.com/buzz.php?p=book'
- r = requests.get(url, headers=header)
- result = re.findall(r'<a class=".*?">(.*?)</a>', r.text)
- print(result)
复制代码
|
|