美文网首页
python 爬取链家

python 爬取链家

作者: zen_shuo | 来源:发表于2017-02-04 18:15 被阅读0次

    from bs4 import BeautifulSoup #导入bs4
    import requests #爬虫请求利器 requests
    csvfile = open("lianjia.csv","a") #新建一个文件
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.98 Safari/537.36 Vivaldi/1.6.689.40'} #模拟浏览器
    for k in range(1,100):#总共100页
    req = requests.get("http://bj.lianjia.com/ershoufang/pg"+str(k),headers = headers)
    req.encoding = ('utf8')
    csvfile = open("lianjia.csv","a")
    soup = BeautifulSoup(req.text, "html.parser")
    for tag in soup.find_all("div","info clear"):
    tag_addr = tag.find("div", "address")
    tag_total_price = tag.find("div","totalPrice")
    tag_unit_price = tag.find("div","unitPrice")
    addr = tag_addr.text.replace("|",",")
    csvfile.write(tag_addr.text +" | " + tag_total_price.text + " | "+ tag_unit_price.text+"\r\n")
    csvfile.close()

    相关文章

      网友评论

          本文标题:python 爬取链家

          本文链接:https://www.haomeiwen.com/subject/ihcsittx.html