温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python58同城爬虫

发布时间:2020-10-22 17:03:52 来源:网络 阅读:405 作者:wx5d72071a58c07 栏目:编程语言
import requests,time,csv
from bs4 import BeautifulSoup

href_list=[]
url='https://hz.58.com/lvguanbinguan'
headers={
        'user-agent': 
                }
params={
        'PGTID': '0d306b32-0004-f711-87c6-3e3cb7371c0d',
        'ClickID': '2'
}
res=requests.get(url,headers=headers,params=params)
soup=BeautifulSoup(res.text,'html.parser')
titles=soup.find_all('h3',class_="title")
for title in titles:
        href=title.find('a')['href']
        print(href)
        href_list.append(href)

csv_file=open('58同城酒店转让项目信息.csv','w+',newline='',encoding='utf-8')
writer=csv.writer(csv_file)
file_head=['项目名','联系人','联系电话','信息','地址']
writer.writerow(file_head)

hotel_list=[]
for href in href_list:
        url=href
        headers={
                'user-agent': 
        }
        res=requests.get(url,headers=headers)
        soup=BeautifulSoup(res.text,'lxml')
        title=soup.find('h2',class_="c_000 f20").text
        name=soup.find(class_="poster-name").text
        phone=soup.find('p',class_="phone-num").text
        detail=soup.find('p',class_="house_basic_title_info").text
        address=soup.find('p',class_="p_2").text
        print(title,phone)
        hotel_list.append([title,name,phone,detail,address])
        time.sleep(1)

for row in hotel_list:
        writer.writerow(row)

csv_file.close()
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI