本来是根据教程上的是区分个人和商家的,但是教程是15年的58同城网站进行了改版,个人的二手物品时跳转转转的,商家的还在58,所以两个详情页面的标签不再通用,没有实现个人和商家页面的跳转。代码如下:
from bs4 import BeautifulSoupimport requestsimport timeurl = ‘http://zhuanzhuan.58.com/detail/883905856824967177z.shtml‘def get_links_from(who_sells): urls = [] list_view = ‘http://bj.58.com/shouji/{}/‘.format(str(who_sells)) web_date = requests.get(list_view) soup = BeautifulSoup(web_date.text,‘lxml‘) for link in soup.select(‘td.t a.t‘): urls.append(link.get(‘href‘)) return urls def get_info(who_sells=0): urls = get_links_from(who_sells) for url in urls: web_date = requests.get(url) soup = BeautifulSoup(web_date.text,‘lxml‘) data = { ‘title‘:soup.title.text, ‘price‘:soup.select(‘span.price_now ‘)[0].text if soup.find_all(‘span‘,‘price_now‘) else None, ‘area‘: soup.select(‘div.palce_li > span > i ‘)[0].text if soup.find_all(‘div‘,‘palce_li‘) else None, ‘catr‘:‘个人‘if who_sells==0 else ‘商家‘, ‘view‘:None, } print(data)get_info()
时间: 2024-10-16 18:44:35