有时候我们并不想要网页中的具体文字内容,而是某一部分的HTML代码:
<div class="pageContent"><p style="text-indent:2em;">南方地区...</p></div>
这时候可以用到 etree.tostring
import requests
from lxml import etree
url = 'http://www.weather.com.cn/index/zxqxgg1/new_wlstyb.shtml'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 QBCore/4.0.1326.400 QQBrowser/9.0.2524.400 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2875.116 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x63010200)'}
res = requests.get(url,headers = headers)
res.encoding = 'utf-8'
html = etree.HTML(res.text)
news_title = html.xpath('/html/body/div[4]/div[1]/div[2]/div/div[1]/span/text()')[0]
content = html.xpath('/html/body/div[4]/div[1]/div[2]/div/div[2]')[0]
# 获取HTML代码
result = etree.tostring(content,encoding='utf-8').decode()
# 对HTML代码增加文字大小
wltq_content = result.replace('class="pageContent"', 'class="pageContent" style=font-size:16px')
print(wltq_content)