看看吧 不懂,帮顶 赞 import requests
from lxml import etree
url='http://www.jk-lawyer.com/detail_6027.html'
resp=requests.get(url)
html=etree.HTML(resp.content.decode('utf-8'))
li=html.xpath("//div[@class='detail']/div[@class='d_p']/*")
for each in li:
title = each.xpath('span/text()')
print(title)
#print(''.join(etree.tostring(tmp).decode() for tmp in li))
我怎么不能回帖呢?
import requests
from lxml import etree
url='http://www.jk-lawyer.com/detail_6027.html'
resp=requests.get(url)
html=etree.HTML(resp.content.decode('utf-8'))
li=html.xpath("//div[@class='detail']/div[@class='d_p']/*")
for each in li:
title = each.xpath('span/text()')
print(title)
#print(''.join(etree.tostring(tmp).decode() for tmp in li))
本帖最后由 chxchxkkk 于 2019-8-11 12:02 编辑
抓取文本没那么复杂,看下面示例:
import requests
from lxml import etree
url = 'http://www.jk-lawyer.com/detail_6027.html'
headers ={'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit' \
'/537.36 (KHTML, like Gecko) ' \
'Chrome/38.0.2125.122 Safari/537.36'}
html = requests.get(url, headers = headers)
html.encoding = 'utf-8'
response = html.text
respons = etree.HTML(response)
respon = respons.xpath('//div[@class="d_p"]/p/span')
for resp in respon:
temptxt = resp.xpath('text()')
print(temptxt)
抓取结果:
D:\program\python37\python.exe F:/pythonlianxi/MyTest/test.py
应泰州市食品药品监督管理局
领导邀请,本所主任王金宝律师于2016年10月19日下午为泰州全市食品药品监管系统食品安全监管人员培训班全体学员以《新食品安全法实用导读》讲授了已于去年10月1日实施的新《食品安全法》,重点讲授了新法背景下食品安全监管的四十要素、新法规定的法律责任概览以及食品安全行政处罚原则等,尤其是针对基层监管执法人员应当掌握和注意的事项。王主任的讲课重点突出,务实致用,且生动活泼,现场互动频繁,受到了泰州市局领导以及全体与会人员的高度评价。
要解决编码问题一句代码就可以了:
html.encoding = 'utf-8'(如果网页是gbk编码,这里utf-8改成gbk就可以了) 顶 chxchxkkk 发表于 2019-8-11 11:58
抓取文本没那么复杂,看下面示例:
import requests
from lxml import etree
获取文本的方式我会,我要的是HTML源码,还是谢谢。不知道兄台有没有其他方法 数据云 发表于 2019-8-11 10:58
我怎么不能回帖呢?
获取文本的方式我会,我要的是HTML源码,还是谢谢。不知道兄台有没有其他方法 帮顶 不懂帮顶
帮顶 帮顶
已解决·· 顶一下
学习
页:
1
[2]