成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術文章
文章詳情頁

網頁爬蟲 - Python3.6 下的爬蟲總是重復爬第一頁的內容

瀏覽:150日期:2022-06-30 17:08:03

問題描述

問題如題:改成while,試了很多,然沒有效果,請教大家

# coding:utf-8# from lxml import etreeimport requests,lxml.html,osclass MyError(Exception): def __init__(self, value):self.value = value def __str__(self):return repr(self.value) def get_lawyers_info(url): r = requests.get(url) html = lxml.html.fromstring(r.content) # phones = html.xpath(’//span[@class='law-tel']’) phones = html.xpath(’//span[@class='phone pull-right']’) # names = html.xpath(’//p[@class='fl']/p/a’) names = html.xpath(’//h4[@class='text-center']’) if(len(phones) == len(names)):list(zip(names,phones))phone_infos = [(names[i].text, phones[i].text_content()) for i in range(len(names))] else:error = 'Lawyers amount are not equal to the amount of phone_nums: '+urlraise MyError(error) phone_infos_list = [] for phone_info in phone_infos:if(phone_info[0] == ''): info = '沒留姓名'+': '+phone_info[1]+'rn'else: info = phone_info[0]+': '+phone_info[1]+'rn'print (info)phone_infos_list.append(info) return phone_infos_listdir_path = os.path.abspath(os.path.dirname(__file__))print (dir_path)file_path = os.path.join(dir_path,'lawyers_info.txt')print (file_path)if os.path.exists(file_path): os.remove(file_path)with open('lawyers_info.txt','ab') as file: for i in range(1000):url = 'http://www.xxxx.com/cooperative_merchants?searchText=&industry=100&provinceId=19&cityId=0&areaId=0&page='+str(i+1)# r = requests.get(url)# html = lxml.html.fromstring(r.content)# phones = html.xpath(’//span[@class='phone pull-right']’)# names = html.xpath(’//h4[@class='text-center']’) # if phones or names:info = get_lawyers_info(url)for each in info: file.write(each.encode('gbk'))

問題解答

回答1:

# coding: utf-8import requestsfrom pyquery import PyQuery as Qurl = ’http://www.51myd.com/cooperative_merchants?industry=100&provinceId=19&cityId=0&areaId=0&page=’with open(’lawyers_info.txt’, ’ab’) as f: for i in range(1, 5):r = requests.get(’{}{}’.format(url, i))usernames = Q(r.text).find(’.username’).text().split()phones = Q(r.text).find(’.phone’).text().split()print zip(usernames, phones)

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 成人毛片1024你懂的 | 91精品久久久| 免费女人18毛片a级毛片视频 | 美女又黄又免费的视频 | 国产精品特黄一级国产大片 | 国产性大片黄在线观看在线放 | 国产三级日产三级日本三级 | 国产精品午夜性视频 | 亚洲国产日韩在线 | 色本道| 美女张开腿让人捅 | 国产综合成人久久大片91 | 成年人在线观看免费 | 精品在线观看免费 | 九一精品国产 | 欧美成人亚洲国产精品 | 欧美一级毛片免费播放器 | 欧美久草| 成人毛片免费免费 | 亚洲日产综合欧美一区二区 | 久久精品视频亚洲 | 国产日本一区二区三区 | 香蕉视频1024 | 欧美极品大肚孕妇孕交 | 国产日产久久 | 国产草草影院ccyycom软件 | 国产精品成人不卡在线观看 | 国产日产亚洲精品 | 久久国内免费视频 | 久久高清精品 | 日韩精品免费一区二区三区 | 毛片在线播放网址 | 草草视频在线播放 | 国产精品青草久久福利不卡 | 美女视频网站永久免费观看软件 | 午夜刺激爽爽视频免费观看 | 一本一本久久a久久精品综合麻豆 | 精品视频在线一区 | 亚洲精品国产成人7777 | 一级毛片真人不卡免费播 | 久久中文字幕久久久久 |