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

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

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

瀏覽:235日期: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 編程
相關文章:
主站蜘蛛池模板: 亚洲图片在线视频 | 黄色国产精品 | 一区视频免费观看 | 日本美女一区 | 精品国产网站 | 久久五月天综合 | 成人亚洲精品 | www.久久综合 | 你懂的在线看 | 女优一区二区三区 | 99热热热热 | 国产精品99久久久久久动医院 | 日日夜夜拍 | 欧美日韩高清 | 日韩久久一区二区三区 | 中文国产视频 | 男人天堂手机在线 | 精品欧美久久 | 激情宗合 | 在线看av网址 | 中文字幕8| 国产激情片 | 久久香蕉网 | 97在线视频免费观看 | 亚洲精品成人在线 | 超碰一区二区三区 | 91九色中文 | 亚洲视频免费观看 | 亚洲日本中文字幕 | www.国产精品.com | 91精品一区二区三区四区 | 黄色小视频在线免费观看 | 91免费观看 | 色婷婷在线播放 | 亚洲在线天堂 | 网曝门在线 | 亚洲精品免费观看 | 国产成人综合精品 | av在线播放免费 | 四虎影视在线播放 | 色视频免费 |