V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
fowill
V2EX  ›  Python

新手求助:关于爬虫 requests 报错“No connection adapters were found” for 的问题

  •  
  •   fowill · 2018-10-30 00:04:45 +08:00 · 12356 次点击
    这是一个创建于 1999 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我正在学习爬虫和 python,还处于萌新阶段,有很多问题还不是很懂。
    写了一个爬小猪短租租房信息的程序,本来 1.0 功能正常,现在想加入点开二级网页再爬取内容的功能,却出现了报错“ No connection adapters were found ” ,实在不清楚出了什么问题。不知道这种报错是哪里出了问题。我实在是比较菜,可能问问题的方式也不对。但有大佬能不吝赐教吗,感激不尽!错误部分代码如下:

    catchNewurl = div.xpath('./div[2]/@detailurl')
    Newurl = catchNewurl
    data4 = requests.get(Newurl).text#此行报错

    完整代码如下
    # -*- coding: utf-8 -*-

    import random
    import time

    import requests
    import xlwt
    from lxml import etree

    #循环外部分准备(输入向导,输入,表头准备)
    print ('请参考城市及拼音缩写:北京市 bj 天津市 tj 沈阳市 sy 长春市 cc 哈尔滨市 heb 上海市 sh 南京市 nj 武汉市 wh 广州市 gz 重庆市 cq 成都市 cd 西安市 xa(冀)石家庄市 sjz,唐山市 ts   (晋)太原市 ty   (蒙)包头市 bt   (辽)大连市 dl,鞍山市 as,抚顺市 fs   (吉)吉林市 jl   (黑)齐齐哈尔市   (苏)徐州市   (浙)杭州市 hz   (闽)福州市 fz   (赣)南昌市 nc   (鲁)济南市 jn 青岛市 qd 淄博市 zb   (豫)郑州市 zz   (湘)长沙市 cs   (贵)贵阳市 gy   (云)昆明市 km   (甘)兰州市 lz   (疆)乌鲁木齐市 wlmq')
    city = input('输入查询城市拼音缩写:')
    writebook = xlwt.Workbook('/Users/wly/Desktop/小猪租房信息.xls')
    sheet = writebook.add_sheet('短租房信息')
    sheet.write(0,0,'名称')
    sheet.write(0,1,'价格')
    sheet.write(0,2,'点评数')
    print ('获取中,请等待')
    x = 1

    #爬虫部分(循环爬取)
    for i in range(1,10):
    url3 = 'http://{}.xiaozhu.com/search-duanzufang-p{}-0/'.format(city,i)
    data3 = requests.get(url3).text #初始化生成一个 XPath 解析对象
    h = etree.HTML(data3)
    home = h.xpath('//*[@id="page_list"]/ul/li')#获取所有标签
    time.sleep(random.randint(5,20))
    for div in home:
    name = div.xpath('./div[2]/div/a/span/text()')[0]
    price = div.xpath('./div[2]/span[1]/i/text()')[0]
    comments = div.xpath('./div[2]/div/em/span/text()')[0].strip()
    catchNewurl = div.xpath('./div[2]/@detailurl')
    Newurl = catchNewurl
    data4 = requests.get(Newurl).text#此行报错
    h1 = etree.HTML(data4)
    newHome = h1.xpath('//*[@id="introducePart"]')
    time.sleep(random.randint(5,20))
    for div in newHome:
    feature1 = div.xpath('./div[0]/div[1]/div/p/text()')[0]
    print(feature1)
    time.sleep(random.randint(5,20))
    sheet.write(x,0,name)
    sheet.write(x,1,price)
    sheet.write(x,2,comments)
    x=x+1
    writebook.save('/Users/wly/Desktop/小猪租房信息.xls')
    11 条回复    2019-06-08 08:54:04 +08:00
    crab
        1
    crab  
       2018-10-30 00:50:23 +08:00   ❤️ 1
    kslr
        2
    kslr  
       2018-10-30 00:51:18 +08:00   ❤️ 1
    真是难为你了,我可以教你上 google
    kslr
        3
    kslr  
       2018-10-30 00:51:51 +08:00   ❤️ 1
    @crab 我正想卖了关子你就发了答案
    inhzus
        4
    inhzus  
       2018-10-30 01:08:15 +08:00   ❤️ 1
    deepdark
        5
    deepdark  
       2018-10-30 08:09:52 +08:00 via Android   ❤️ 1
    真是难为你了,我可以教你上 google
    itskingname
        6
    itskingname  
       2018-10-30 08:27:58 +08:00 via iPhone   ❤️ 1
    真是难为你了,你可以看我写的爬虫书: https://item.jd.com/12436581.html
    fowill
        7
    fowill  
    OP
       2018-10-30 10:23:35 +08:00
    @crab @deepdark @inhzus @kslr @itskingname 各位大哥,其实我 Google 过的,但是不是少 http://的问题。我还是自己继续找一下吧,谢谢各位了。主要还是自己太菜。
    ClutchBear
        8
    ClutchBear  
       2018-10-30 20:21:11 +08:00


    catchNewurl 这个是一个列表,你要用的是其中的一个元素
    ClutchBear
        9
    ClutchBear  
       2018-10-30 20:21:33 +08:00   ❤️ 1

    上一个贴图错误
    xueyoucai
        10
    xueyoucai  
       2018-10-31 08:47:25 +08:00
    真是难为你了,我可以手把手教你上百度以及 google
    atywz
        11
    atywz  
       2019-06-08 08:54:04 +08:00
    人家问题描述这么详细 一个个阴阳怪气的 真是够了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1085 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:21 · PVG 07:21 · LAX 16:21 · JFK 19:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.