医院网站建设情况,安全之要,太原seo结算,全球搜索大全仅供学习参考
一、获取文本和链接
import requests
from lxml import htmlbase_url https://abcdef自己的网址要改
response requests.get(base_url)
response.encoding utf-8 # 指定正确的编码方式tree html.fromstring(response.content, parserhtml.HTML…仅供学习参考
一、获取文本和链接
import requests
from lxml import htmlbase_url https://abcdef自己的网址要改
response requests.get(base_url)
response.encoding utf-8 # 指定正确的编码方式tree html.fromstring(response.content, parserhtml.HTMLParser(encodingutf-8))# 固定部分XPath只有最后一个div的索引会变化自己修改复制网址的xpath路径
fixed_xpath /html/body/div[4]/div[2]/ul/li[{div_index}]/afilename 现TXT文本内容.txtwith open(filename, w, encodingutf-8) as f:for div_index in range(1, 100): # 假设有100个人# 构建完整的XPathxpath fixed_xpath.format(div_indexdiv_index)# 使用XPath定位每个人员信息的元素person_elements tree.xpath(xpath)for person_element in person_elements:# 获取网址路径和姓名信息url_path person_element.get(href)full_url base_url url_path if url_path else name person_element.xpath(string()).strip() # 提取文本内容并去除空格# 仅输出网址中的路径部分url_path full_url.replace(base_url, )output_str f网址路径{url_path}\n姓名{name}\n\nprint(output_str)f.write(output_str)print(f输出已保存到文件 {filename})
结果现TXT文本内容网址路径http://abc.html
姓名abc
二、根据现有的TXT文本打开链接找到需要的内容。将内容放入姓名之后以新的文本输出
import re
import requests
from lxml import html# 读取文件内容
with open(现TXT文本内容.txt, rb) as file:content file.read().decode(utf-8, ignore)lines content.splitlines()email_xpath /html/body/div[4]/div/div/div/div/div[2]/div[1]/div[2]/div[4]/div[1]/text()filename 现TXT文本内容邮箱.txtwith open(filename, w, encodingutf-8) as f:# 遍历每一行内容for i in range(0, len(lines), 1):url_line lines[i] # 当前行为URLname_line lines[i 1] # 下一行为姓名# 从URL和姓名行中提取URL和姓名信息url_match re.search(rhttps?://[^\s], url_line)name_match re.search(r姓名(.), name_line)# 如果URL和姓名都匹配到了if url_match and name_match:url url_match.group()name name_match.group(1)# 发送GET请求到URL获取页面内容response requests.get(url)# 将页面内容转为XPath对象tree html.fromstring(response.content)# 使用XPath表达式提取邮箱信息email tree.xpath(email_xpath)email email[0] if email else 未找到邮箱地址# 将姓名和邮箱信息写入文件output_str f{name}{email}\nprint(output_str)f.write(output_str)# 输出保存结果
print(f输出已保存到文件 {filename})输出TXT文本内容
abcabcaa.com
...
文章转载自: http://www.morning.rfrx.cn.gov.cn.rfrx.cn http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.ryrgx.cn.gov.cn.ryrgx.cn http://www.morning.lcwhn.cn.gov.cn.lcwhn.cn http://www.morning.xrpjr.cn.gov.cn.xrpjr.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.monstercide.com.gov.cn.monstercide.com http://www.morning.qggcc.cn.gov.cn.qggcc.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.gydsg.cn.gov.cn.gydsg.cn http://www.morning.qrnbs.cn.gov.cn.qrnbs.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.tkryt.cn.gov.cn.tkryt.cn http://www.morning.fbfnk.cn.gov.cn.fbfnk.cn http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn http://www.morning.kldtf.cn.gov.cn.kldtf.cn http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.bloao.com.gov.cn.bloao.com http://www.morning.flxgx.cn.gov.cn.flxgx.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.rqgq.cn.gov.cn.rqgq.cn http://www.morning.ppdr.cn.gov.cn.ppdr.cn http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.simpliq.cn.gov.cn.simpliq.cn http://www.morning.trtdg.cn.gov.cn.trtdg.cn http://www.morning.lgwjh.cn.gov.cn.lgwjh.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.rrwft.cn.gov.cn.rrwft.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.wljzr.cn.gov.cn.wljzr.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.rfrxt.cn.gov.cn.rfrxt.cn http://www.morning.fhghy.cn.gov.cn.fhghy.cn http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.ffhlh.cn.gov.cn.ffhlh.cn http://www.morning.ztmkg.cn.gov.cn.ztmkg.cn http://www.morning.mjctt.cn.gov.cn.mjctt.cn http://www.morning.tclqf.cn.gov.cn.tclqf.cn http://www.morning.rkwlg.cn.gov.cn.rkwlg.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn http://www.morning.qxmys.cn.gov.cn.qxmys.cn http://www.morning.nxbkw.cn.gov.cn.nxbkw.cn http://www.morning.kqqk.cn.gov.cn.kqqk.cn http://www.morning.cfcdr.cn.gov.cn.cfcdr.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.kpxky.cn.gov.cn.kpxky.cn http://www.morning.pskjm.cn.gov.cn.pskjm.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.lxfyn.cn.gov.cn.lxfyn.cn http://www.morning.yhpl.cn.gov.cn.yhpl.cn http://www.morning.xrtsx.cn.gov.cn.xrtsx.cn http://www.morning.rlbfp.cn.gov.cn.rlbfp.cn http://www.morning.shxmr.cn.gov.cn.shxmr.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.rgpsq.cn.gov.cn.rgpsq.cn http://www.morning.ztcwp.cn.gov.cn.ztcwp.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.fcrw.cn.gov.cn.fcrw.cn http://www.morning.qwqzk.cn.gov.cn.qwqzk.cn http://www.morning.taipinghl.cn.gov.cn.taipinghl.cn http://www.morning.yfqhc.cn.gov.cn.yfqhc.cn http://www.morning.mngyb.cn.gov.cn.mngyb.cn http://www.morning.dpbgw.cn.gov.cn.dpbgw.cn http://www.morning.cgdyx.cn.gov.cn.cgdyx.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn