使用包含Python和beautifulsoup的url的.txt文件从多个网页中获取数据

2024-09-30 10:40:02 发布

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

我有一个.txt文件,其中包含指向多个页面的完整URL,每个页面都包含一个我想从中获取数据的表。我的代码适用于一个URL,但是当我试图添加一个循环并从.txt文件中读取URL时,我得到以下错误

raise ValueError, "unknown url type: %s" % self.__original
ValueError: unknown url type: ?

这是我的密码

^{pr2}$

我检查了我的.txt文件,所有的条目都是正常的。它们以HTTP:开头,以.html结尾。它们周围没有撇号或引号。我是不是把for循环编码错了?

使用

with open('urls.txt', 'r') as f:
    for url in f:
        print(url)

我得到以下信息

??http://www.thegreenpapers.com/PCC/AL-D.html

http://www.thegreenpapers.com/PCC/AL-R.html

http://www.thegreenpapers.com/PCC/AK-D.html

以此类推100行。只有第一行有问号。 我的.txt文件包含那些只改变州和党的缩写的url。


Tags: 文件txtcomhttpurlforhtmlwww
2条回答

您尝试的方式可以通过在代码中抽动两个不同的行来修复。在

试试这个:

with open('urls.txt', 'r') as f:
    urls = f.readlines()   #make sure this line is properly indented.
for url in urls:
    uClient = urlopen(url.strip())

不能使用“f.read()”将整个文件读入字符串,然后在字符串上迭代。要解决此问题,请参阅下面的更改。我也删除了你的最后一行。当您使用'with'语句时,它将在代码块完成时关闭文件。在

Code from Greg Hewgillfor(Python 2)显示url字符串的类型是“str”还是“unicode”。在

from urllib2 import urlopen
from bs4 import BeautifulSoup as soup

# Code from Greg Hewgill
def whatisthis(s):
    if isinstance(s, str):
        print "ordinary string"
    elif isinstance(s, unicode):
        print "unicode string"
    else:
        print "not a string"

with open('urls.txt', 'r') as f:
    for url in f:
        print(url)
        whatisthis(url)
        uClient = urlopen(url)
        page_html = uClient.read()
        uClient.close()

        page_soup = soup(page_html, "html.parser")

        containers = page_soup.findAll("tr", {"class":"data"})

        for container in containers:
            unform_name = container.findAll("th", {"width":"30%"})
            name = unform_name[0].text.strip()

            unform_delegate = container.findAll("td", {"id":"y000"})
            delegate = unform_delegate[0].text.strip()

            print(name)
            print(delegate)

使用具有上面列出的URL的文本文件运行代码将生成以下输出:

^{pr2}$

相关问题 更多 >

    热门问题