首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python读取嵌入代码,提取url并将url标题写入新的csv文件

Python读取嵌入代码,提取URL并将URL标题写入新的CSV文件的过程可以通过以下步骤完成:

  1. 导入所需的Python库:
代码语言:txt
复制
import re
import csv
import requests
from bs4 import BeautifulSoup
  1. 定义一个函数来提取URL和标题:
代码语言:txt
复制
def extract_url_title(embedded_code):
    urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', embedded_code)
    titles = []
    for url in urls:
        response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')
        title = soup.title.string if soup.title else ''
        titles.append(title)
    return urls, titles
  1. 读取嵌入代码文件并调用函数提取URL和标题:
代码语言:txt
复制
embedded_code_file = 'embedded_code.txt'
output_file = 'output.csv'

with open(embedded_code_file, 'r') as file:
    embedded_code = file.read()

urls, titles = extract_url_title(embedded_code)
  1. 将提取的URL和标题写入CSV文件:
代码语言:txt
复制
with open(output_file, 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['URL', 'Title'])
    for url, title in zip(urls, titles):
        writer.writerow([url, title])

完整的Python代码如下:

代码语言:txt
复制
import re
import csv
import requests
from bs4 import BeautifulSoup

def extract_url_title(embedded_code):
    urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', embedded_code)
    titles = []
    for url in urls:
        response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')
        title = soup.title.string if soup.title else ''
        titles.append(title)
    return urls, titles

embedded_code_file = 'embedded_code.txt'
output_file = 'output.csv'

with open(embedded_code_file, 'r') as file:
    embedded_code = file.read()

urls, titles = extract_url_title(embedded_code)

with open(output_file, 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['URL', 'Title'])
    for url, title in zip(urls, titles):
        writer.writerow([url, title])

这段代码通过正则表达式提取嵌入代码中的URL,然后使用requests库发送HTTP请求获取网页内容。使用BeautifulSoup库解析网页内容,提取标题。最后,将URL和标题写入CSV文件中。

推荐的腾讯云相关产品:腾讯云对象存储(COS),用于存储和管理文件、图片、视频等静态资源。产品介绍链接地址:https://cloud.tencent.com/product/cos

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

Python处理CSV文件(一)

CSV(comma-separated value,逗号分隔值)文件格式是一种非常简单的数据存储与分享方式。CSV 文件将数据表格存储为纯文本,表格(或电子表格)中的每个单元格都是一个数值或字符串。与 Excel 文件相比,CSV 文件的一个主要优点是有很多程序可以存储、转换和处理纯文本文件;相比之下,能够处理 Excel 文件的程序却不多。所有电子表格程序、文字处理程序或简单的文本编辑器都可以处理纯文本文件,但不是所有的程序都能处理 Excel 文件。尽管 Excel 是一个功能非常强大的工具,但是当你使用 Excel 文件时,还是会被局限在 Excel 提供的功能范围内。CSV 文件则为你提供了非常大的自由,使你在完成任务的时候可以选择合适的工具来处理数据——如果没有现成的工具,那就使用 Python 自己开发一个!

01

《Learning Scrapy》(中文版)第5章 快速构建爬虫一个具有登录功能的爬虫使用JSON APIs和AJAX页面的爬虫在响应间传递参数一个加速30倍的项目爬虫可以抓取Excel文件的爬虫总结

第3章中,我们学习了如何从网页提取信息并存储到Items中。大多数情况都可以用这一章的知识处理。本章,我们要进一步学习抓取流程UR2IM中两个R,Request和Response。 一个具有登录功能的爬虫 你常常需要从具有登录机制的网站抓取数据。多数时候,网站要你提供用户名和密码才能登录。我们的例子,你可以在http://web:9312/dynamic或http://localhost:9312/dynamic找到。用用户名“user”、密码“pass”登录之后,你会进入一个有三条房产链接的网页。现在的问

08
领券