午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

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

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

瀏覽:244日期: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 編程
相關文章:
主站蜘蛛池模板: 欧美日韩一级二级 | 久久国产视频一区 | 特级西西人体444www高清大胆 | 国产999久久久 | 天堂久久av | 成人免费在线观看 | 中文字幕免费 | av撸撸在线 | 色综合区 | 欧美亚洲综合在线 | 日韩一区二区三区四区 | 美国一级片在线观看 | 黄色国产视频 | 免费视频色 | 玖玖综合网 | 国产一级二级三级在线观看 | 亚洲23p| jlzzjlzzjlzz亚洲人 | 成人免费区一区二区三区 | 婷婷视频在线观看 | 黄网在线 | 五月婷婷免费视频 | 成年人在线视频观看 | 午夜黄视频| 天天插天天搞 | 亚洲第九十九页 | 欧美精品另类 | 成人日皮视频 | 久久久99国产精品免费 | 欧美性高潮 | 欧美精品激情 | a在线播放 | 国产夫绿帽单男3p精品视频 | 黄色一级片在线播放 | 亚洲综合激情网 | 免费看成人片 | 免费黄色欧美 | 天天操天天爱天天干 | 激情五月色播五月 | 亚洲欧美另类日韩 | 成年人的毛片 |