-- coding: utf-8 --
import json
import requests
from lxml import html
etree = html.etree
def baidu_request(keyword):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55',
'Accept': 'image/webp,image/apng,image/svg+xml,image/,/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Connection': 'keep-alive',
'Content - Type': 'text / html;charset = utf - 8',
}
response = requests.get('https://www.baidu.com/s?wd={}&lm=1%27.format(keyword), headers=headers)
r = response.text
html = etree.HTML(r, etree.HTMLParser())
r1 = html.xpath('//h3')
r2 = html.xpath('//*[@class="c-abstract"]')
r3 = html.xpath('//*[@class="t"]/a/@href')
for i in range(5):
r11 = r1[i].xpath('string(.)')
r22 = r2[i].xpath('string(.)')
r33 = r3[i]
with open('test.txt', 'a', encoding='utf-8') as c:
c.write(json.dumps(r11, ensure_ascii=False) + '\n')
c.write(json.dumps(r22, ensure_ascii=False) + '\n')
c.write(json.dumps(r33, ensure_ascii=False) + '\n')
print(r11, '\n')
print('------------------------')
print(r22, '\n')
print(r33, '\n')
if name == 'main':
baidu_request("安全事件")