Python爬虫html网址实战笔记

仅供学习参考

一、获取文本和链接

c 复制代码
import requests
from lxml import html

base_url = "https://abcdef自己的网址要改"
response = requests.get(base_url)
response.encoding = 'utf-8'  # 指定正确的编码方式

tree = html.fromstring(response.content, parser=html.HTMLParser(encoding='utf-8'))

# 固定部分XPath,只有最后一个div的索引会变化,自己修改,复制网址的xpath路径
fixed_xpath = "/html/body/div[4]/div[2]/ul/li[{div_index}]/a"

filename = "现TXT文本内容.txt"

with open(filename, "w", encoding="utf-8") as f:
    for div_index in range(1, 100):  # 假设有100个人
        # 构建完整的XPath
        xpath = fixed_xpath.format(div_index=div_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\n"
            print(output_str)
            f.write(output_str)

print(f"输出已保存到文件 {filename}")
c 复制代码
结果:现TXT文本内容

网址路径:http://abc.html
姓名:abc

二、根据现有的TXT文本,打开链接找到需要的内容

c 复制代码
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文本内容邮箱.txt"

with open(filename, "w", encoding="utf-8") as f:
    for i in range(0, len(lines), 1):
        url_line = lines[i]
        name_line = lines[i + 1]

        url_match = re.search(r"https?://[^\s]+", url_line)
        name_match = re.search(r"姓名:(.+)", name_line)

        if url_match and name_match:
            url = url_match.group()
            name = name_match.group(1)

            response = requests.get(url)
            tree = html.fromstring(response.content)

            email = tree.xpath(email_xpath)
            email = email[0] if email else "未找到邮箱地址"

            output_str = f"{name}:{email}\n"
            print(output_str)
            f.write(output_str)

print(f"输出已保存到文件 {filename}")
c 复制代码
输出TXT文本内容
abc:abc@aa.com
...
...
相关推荐
久未36 分钟前
Pytorch autoload机制自动加载树外扩展(Autoload Device Extension)
人工智能·pytorch·python
rising start1 小时前
前端基础一、HTML5
前端·html·html5
java1234_小锋1 小时前
TensorFlow2 Python深度学习 - TensorFlow2框架入门 - 使用Keras.Model来定义模型
python·深度学习·tensorflow·tensorflow2
Learn Beyond Limits1 小时前
TensorFlow Implementation of Content-Based Filtering|基于内容过滤的TensorFlow实现
人工智能·python·深度学习·机器学习·ai·tensorflow·吴恩达
java1234_小锋1 小时前
TensorFlow2 Python深度学习 - 函数式API(Functional API)
python·深度学习·tensorflow·tensorflow2
Never_Satisfied1 小时前
在JavaScript / HTML中,div容器在内容过多时不显示超出的部分
开发语言·javascript·html
Y200309161 小时前
使用 PyTorch 实现 MNIST 手写数字识别
python
马尚来1 小时前
移动端自动化测试Appium,从入门到项目实战Python版
python
天才测试猿1 小时前
WebUI自动化测试:POM设计模式全解析
自动化测试·软件测试·python·selenium·测试工具·设计模式·测试用例
MonkeyKing_sunyuhua1 小时前
python线程间怎么通信
android·网络·python