yzhu1015 2018-07-12 14:53 采纳率: 0%
浏览 2425
已结题

在python爬虫中使用for循环填入网页id并嵌套函数爬取信息时,无法正确爬取

使用了三个网页ID:38635、38636、38627来测试,输出dic的结果为三个同样的字典(都对应38635),

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
url='https://db.yaozh.com/hmap?grade=%E5%85%A8%E9%83%A8&p={}&pageSize=30&province=%E5%B9%BF%E4%B8%9C%E7%9C%81&type=%E5%85%A8%E9%83%A8'
info_url_model='https://db.yaozh.com{}'


for x in ['/hmap/38625.html', '/hmap/38626.html', '/hmap/38627.html']:  #test
        info_url=info_url_model.format(x)      #填入医院urlID    
        detail = requests.get(info_url, headers=headers)  # 取得内容页面内容
        detail.encodint = 'utf-8'
        soup2 = BeautifulSoup(detail.text, 'html.parser')  
        a = soup2.select('.toFindImg')
        for b in soup2.find_all(lambda tag: tag.name == 'span' and tag.get('class') == ['toFindImg']):
            item_list.append(b.text)
            for i in range(len(item_list)):                           #去掉空格
                item_list[i] = item_list[i].replace(' ', '')          #去掉空格
                item_list[i] = item_list[i].replace('\n', '')       #去掉换行符
            dic = dict(zip(item_name, item_list))  # 生成字典
        print(dic)
  • 写回答

5条回答 默认 最新

  • pqq48006106 2018-07-13 00:21
    关注

    可以试试java的爬虫框架gecco

    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程