如何访问excel文件中的url,并使用beautifulsoup从这些链接中获取存储的信息?

2024-05-14 10:16:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图访问一组行中的url,并从所有这些链接中获取各自的信息并将其存储在文本文件中。我把我的链接存储在一个文件-“ctp_输出.csv“ 目前我可以通过直接提供一个链接来提取信息。需要一些指导。在

import csv
import urllib2
from bs4 import BeautifulSoup
url = "http://www.thedrum.com/news/2015/07/29/mankind-must-get-ahead-technical-development-states-phds-mark-holden-following"

soup = BeautifulSoup(urllib2.urlopen(url))
with open('ctp_output.txt', 'w') as f:
    for tag in soup.find_all('p'):
        f.write(tag.text.encode('utf-8') + '\n')

Tags: 文件csvfromimport信息url链接tag
2条回答

下一步是打开csv文件,然后遍历每一行,提取每个链接的信息。你可以这样做:

import csv

with open('test.csv', 'rb') as f:
    reader = csv.reader(f)
    for line in reader:
        url = line[0] # assuming your url is your first column
        .... # scraping code here

可以使用pandas.read_csv()pandas数据帧中导入csv。 然后迭代数据帧的行,如下所示

for url in data_frame_name.iterrows():
....use the url to get the information like you did in the question.

相关问题 更多 >

    热门问题