簡體   English   中英

如何從 URL 中提取數據?

[英]How do I extract data from the URLs?

我有一個 xlsx 文件,其中存儲了許多 URL 及其序列號。 這些 URL 中的每一個都重定向到寫有文章的網頁。 我的問題是如何使用 python 掃描所有 URL 並將文章的標題和文本存儲在以 URL 序列號作為文件名的新文本文件中?

您可以使用網絡抓取來做到這一點。

正如你所說,你有一個包含元組(ids, url)的 xlsx 。

您可以首先將其加載到 python 中:

import pandas as pd

urls = pd.read_excel(filename)

然后要閱讀每個 URL 的內容,您可以使用 Python 中最著名的 Web 抓取庫之一: BeautifulSoup

from bs4 import BeautifulSoup
import requests

# get the raw HTML from the request
content = requests.get(url).content

# build the soup
soup = BeautifulSoup(content)

# get the title
title_tag = soup.find("title") # shows <title>ActualTitle</title>

title = soup.find("title").string # show ActualTitle


# You can get the whole text contained in the page 
text_content = soup.get_text()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM