首页 > 其他 > 详细

爬虫(二)

时间:2019-05-21 18:20:58      阅读:129      评论:0      收藏:0      [点我收藏+]

  在开发工具内获取“请求头”来伪装成浏览器,以便更好地抓取数据

技术分享图片

 

 

!/usr/bin/env python
-*- encoding:UTF-8 -*-

import requests

headers = {
    ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36‘
}
res = requests.get(‘http://bj.xiaozhu.com/‘,headers=headers)    # get方法加入请求头
try:
    print(res.text)
except ConnectionError:
    print(‘拒绝连接‘)


# 通过BeautiSoup库解析得到的Soup文档是标准结构化数据比上面的更好
import requests
from bs4 import BeautifulSoup

headers = {
    ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36‘
}
res = requests.get(‘http://bj.xiaozhu.com/‘,headers=headers)    # get方法加入请求头
try:
    soup = BeautifulSoup(res.text, ‘html.parser‘)
    print(soup.prettify())
except ConnectionError:
    print(‘拒绝连接‘)

技术分享图片

技术分享图片

更新后:

price = soup.select(‘#page_list > ul > li:nth-child(1) > div.result_btm_con.lodgeunitname > div:nth-child(1) > ‘
                    ‘span.result_price > i‘)

 

爬虫(二)

原文:https://www.cnblogs.com/King-boy/p/10901389.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!