更新bs4库至4.7.0以上,然后就可以使用了。(推荐)
pip install --upgrade beautifulsoup4
比如这个规则,bs4 解析不出来, 会报错 需要将nth-child改为 nth-of-type,
虽然这样不会报错了,但是解析的位置不对
div.show-status > span:nth-child(3)
解决方法:
调用Scrapy 的 css 选择器
import requests
from scrapy.selector import Selector
url = 'http://www.tchuan.gov.cn/show/2019/03/07/36310.html'
# ['h1.show-title', 'div.show-status > span:nth-child(3)', '#content']
html = requests.get(url)
a = Selector(text = html.text)
b = a.css('div.show-status > span:nth-child(3)').extract()[0]
print(a)
print(b)