- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在构建网络 scraper 以编译数据并输出为 XLS 格式时遇到错误;当再次测试我希望从中抓取的域列表时,程序在收到套接字错误时出错。希望找到一个“if”语句,该语句将使解析损坏的网站无效并继续我的 while 循环。有什么想法吗?
workingList = xlrd.open_workbook(listSelection)
workingSheet = workingList.sheet_by_index(0)
destinationList = xlwt.Workbook()
destinationSheet = destinationList.add_sheet('Gathered')
startX = 1
startY = 0
while startX != 21:
workingCell = workingSheet.cell(startX,startY).value
print ''
print ''
print ''
print workingCell
#Setup
preSite = 'http://www.'+workingCell
theSite = urlopen(preSite).read()
currentSite = BeautifulSoup(theSite)
destinationSheet.write(startX,0,workingCell)
这里是错误:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
homeMenu()
File "C:\Python27\farming.py", line 31, in homeMenu
openList()
File "C:\Python27\farming.py", line 79, in openList
openList()
File "C:\Python27\farming.py", line 83, in openList
openList()
File "C:\Python27\farming.py", line 86, in openList
homeMenu()
File "C:\Python27\farming.py", line 34, in homeMenu
startScrape()
File "C:\Python27\farming.py", line 112, in startScrape
theSite = urlopen(preSite).read()
File "C:\Python27\lib\urllib.py", line 84, in urlopen
return opener.open(url)
File "C:\Python27\lib\urllib.py", line 205, in open
return getattr(self, name)(url)
File "C:\Python27\lib\urllib.py", line 342, in open_http
h.endheaders(data)
File "C:\Python27\lib\httplib.py", line 951, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 811, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 773, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 754, in connect
self.timeout, self.source_address)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno 11004] getaddrinfo failed
最佳答案
嗯,这看起来像是我在互联网连接中断时遇到的错误。 HTTP 404 错误是当您有连接但找不到您指定的 URL 时遇到的错误。
没有 if 语句来处理异常;你需要使用 try/except construct. 来“捕捉”它们
更新:这是一个演示:
import urllib
def getconn(url):
try:
conn = urllib.urlopen(url)
return conn, None
except IOError as e:
return None, e
urls = """
qwerty
http://www.foo.bar.net
http://www.google.com
http://www.google.com/nonesuch
"""
for url in urls.split():
print
print url
conn, exc = getconn(url)
if conn:
print "connected; HTTP response is", conn.getcode()
else:
print "failed"
print exc.__class__.__name__
print str(exc)
print exc.args
输出:
qwerty
failed
IOError
[Errno 2] The system cannot find the file specified: 'qwerty'
(2, 'The system cannot find the file specified')
http://www.foo.bar.net
failed
IOError
[Errno socket error] [Errno 11004] getaddrinfo failed
('socket error', gaierror(11004, 'getaddrinfo failed'))
http://www.google.com
connected; HTTP response is 200
http://www.google.com/nonesuch
connected; HTTP response is 404
请注意,到目前为止我们只是打开了连接。现在您需要做的是检查 HTTP 响应代码并确定是否有任何值得使用 conn.read()
关于Python Scraper - 如果目标是 404,则套接字错误会中断脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8860200/
我需要创建一个脚本来发出 HTTP 请求并模拟浏览器在 cookie 管理方面的行为。这意味着它可以访问该“页面”设置的所有 cookie(服务器设置 cookie、异步客户端设置 cookie、每个
我有一个简单的 JavaScript 抓取工具,用于本地页面。 我会找到所有“title”类元素的 id 属性。 问题是,当我在屏幕上打印变量时,变量(title1)永远是“未定义”,而不是像它必须的
我编写了一些代码来解析来自 yell.com 的不同商店的名称、地址和电话号码。如果为我的爬虫提供了任何链接,它就会解析整个内容,而不管它分布在多少页面上。然而,我能发现的唯一问题是它总是跳过第一页的
我用 python 结合 BeautifulSoup 编写了一个脚本,使用其分页按钮转到网站的下一页 (有一个链接连接到这个按钮)直到没有新的页面可供抓取。我的脚本可以使用分页链接抓取下一页。然而,问
我用 python 编写了一个脚本,用于从 craigslist 中抓取五个项目的“姓名”和“电话”。我面临的问题是,当我运行脚本时,它只给出三个结果而不是五个结果。更具体地说,由于前两个链接的页面中
我用 python 结合 selenium 编写了一个脚本来解析网页中的名称。该网站的数据未启用 JavaScript。然而,下一页链接是在 javascript 内的。由于如果我使用 request
我在 python 中编写了一个小脚本,使用 xpath 从 yahoo finance 中抓取显示在左侧区域的标题。该页面中有几个标题,但是当我运行我的脚本时,我只得到三个标题。我不想在这种情况下使
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我用 python 结合 Selenium 编写了一个脚本,以提取 finance.yahoo 网站左侧栏中显示的不同新闻的标题。我已经使用 css 选择器来获取内容。但是,该脚本既没有给出任何结果也
我用 python 结合 selenium 编写了一个脚本,以便在搜索时显示一些名称和相应的地址,搜索关键字是“Saskatoon”。但是,在这种情况下,数据会遍历多个页面。除了一件事,我的脚本几乎完
例如,我有下一张 table First 1 Second 2
想要在某些 channel 统计数据中删除youtube,并了解其工作原理。并且比作csv作以下分析。使用this video创建和学习。 我有2个文件: main.py,youtube_statis
我正在尝试抓取此页面 https://www.teamrankings.com/nba/team/cleveland-cavaliers但我需要当您单击“投注 View ”时显示的网格...我遇到了一
我编写了一个抓取工具来解析来自 torrent 站点的电影信息。我使用了 IE 和 queryselector。 我的代码确实解析了所有内容。当一切完成后,它会抛出错误而不是退出浏览器。如果我取消错误
我正在尝试创建一个在名为 https://en.wikipedia.org/wiki/North_Korea_and_weapons_of_mass_destruction 的维基百科页面上启动的蜘蛛
我正在尝试抓取this网站使用 python 为献血营准备数据库。 首先,在尝试从 requests 或 urllib 获取网站 html 源代码时,存在 SSl:certificate_verify
我正在尝试使用允许空格和句点的正则表达式编写一个 python scraper。 我正在尝试从某个网站获取棒球队名称,因此有些是 Miami (全字),但其他是 San Francisco (带空格)
我已经用Python创建了一个网络爬虫,但是在最后打印时我想打印我已经下载的(“Bakerloo:”+ info_from_website),正如你在代码中看到的那样,但它总是像info_from_w
我有这个代码: #!/opt/local/bin/perl use 5.014; use warnings; use Web::Scraper; use Data::Dumper; my $html
我正在尝试使用 Scrapy 来抓取该网站。 首先这是我的代码-: from twisted.internet import reactor from scrapy.crawler import Cr
我是一名优秀的程序员,十分优秀!