#coding:utf8
import os
#判断文件中是否包含关键字,是则将文件路径打印出来
def is_file_contain_word(file_list, query_word):
for _file in file_list:
if query_word in open(_file).read():
print _file
print("Finish searching.")
#返回指定目录的所有文件(包含子目录的文件)
def get_all_file(floder_path):
file_list = []
if floder_path is None:
raise Exception("floder_path is None")
for dirpath, dirnames, filenames in os.walk(floder_path):
for name in filenames:
file_list.append(dirpath + '\\' + name)
return file_list
query_word = raw_input("Please input the key word that you want to search:")
basedir = raw_input("Please input the directory:")
is_file_contain_word(get_all_file(basedir), query_word)
raw_input("Press Enter to quit.")
请采纳
Copyright © 1999-2020, CSDN.NET, All Rights Reservedpython
打开APP
关注
学习日志:Python 实现网络爬虫——提取关键字 原创
2022-06-19 13:02:38
小羊努力搞代码
码龄174天
关注
编写一段Python代码,向百度提交查询关键词“桃花源记”,抓取百度的查询结果,要求有文字、链接,可以在浏览器中打开抓取的链接,或者调用浏览器打开抓取的链接。
红框内是根据网站信息需要更改的内容。57031baa3a394395be479ad89f1ff15e.png
附上完整代码:
import json
import requests
from lxml import etree
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/88.0.4324.104 Safari/537.36"
}
response = requests.get('https://www.baidu.com/s?wd=桃花源记&lm=0', headers=headers)
r = response.text
html = etree.HTML(r, etree.HTMLParser())
r1 = html.xpath('//h3')
r2 = html.xpath('//*[@class="content-right_8Zs40"]')
r3 = html.xpath('//*[@class="c-row source_1Vdff OP_LOG_LINK c-gap-top-xsmall"]/a/@href')
for i in range(4):
r11 = r1[i].xpath('string(.)')
r22 = r2[i].xpath('string(.)')
r33 = r3[i]
with open('桃花源记.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, end='\n')
print('------------------------')
print(r22, end='\n')
print(r33)