像往常一样,我经常遇到问题,我彻底地寻找了当前问题的答案,但发现自己不知所措。以下是我搜索过的一些地方:- How to fix httplib.BadStatusLine exception? - Python httplib2 Handling Exceptions - python http status code
我的问题如下。我已经创建了一个蜘蛛,并想抓取不同的网址。当我独立抓取每个网址时,一切都很正常。然而,当我尝试抓取两者时,我得到了以下错误:httplib.BadStatusLine: ''
我遵循了我读到的一些建议(参见上面提到的链接),可以打印每个请求的response.status,但是response.url没有打印,并且抛出了错误。(我打印这两条语句只是为了找出错误的根源)。
我希望这一点是清楚的。
我在用scrapy和selenium
class PeoplePage(Spider):
name = "peopleProfile"
allowed_domains = ["blah.com"]
handle_httpstatus_list = [200, 404]
start_urls = [
"url1",
"url2"
]
def __init__(self):
self.driver = webdriver.Firefox()
def parse(self, response):
print response.status
print '???????????????????????????????????'
if response.status == 200:
self.driver.implicitly_wait(5)
self.driver.get(response.url)
print response.url
print '!!!!!!!!!!!!!!!!!!!!'
# DO STUFF
self.driver.close()发布于 2014-12-23 20:56:21
基于Python Doc,如果服务器使用我们不理解的HTTP状态码响应时,将引发httplib.BadStatusLine。您可以尝试传递此异常。如果要调用多个url,则不应关闭驱动程序。
试试这个:
def parse(self, response):
try:
print response.status
print '???????????????????????????????????'
if response.status == 200:
self.driver.implicitly_wait(5)
self.driver.get(response.url)
print response.url
print '!!!!!!!!!!!!!!!!!!!!'
# DO STUFF
except httplib.BadStatusLine:
pass发布于 2017-03-14 08:15:57
我做了一个装饰器来做顶部答案所做的事情,以便使代码易于重用。这就是它:
import http
def pass_bad_status_line_exc(wrapped_function):
"""
Silently pass this exception `http.client.BadStatusLine` decorator
"""
def _wrapper(*args, **kwargs):
try:
result = wrapped_function(*args, **kwargs)
except http.client.BadStatusLine:
return
return result
return _wrapper发布于 2018-04-20 00:00:39
我遇到这个错误是因为我定义了一个selenium.webdriver实例(名为driver),在它上面调用了driver.quit(),然后尝试在退出驱动程序上调用driver.get(url)。解决方案是不调用driver.quit()。
https://stackoverflow.com/questions/27619258
复制相似问题