python爬虫抓取91处理网

本人是个爬虫小萌新,看了网上教程学着做爬虫爬取91处理网www.91chuli.com,如果有什么问题请大佬们反馈,谢谢。

以下是用lxml来爬取的。

from lxml import etree

def getHTMLText(url):
    kv = {
        'cookie': 'ssids=1581214855718752; sfroms=JIAOYIMALL001; historyScanGame=%5B%225667%22%2Cnull%5D; session=1581214855718753-7; showFixGuideDialog=true'
        , 'user-agent': 'Mozilla/5.0'}
    r = requests.get(url, headers=kv)
    r.raise_for_status()
    r.encoding = 'utf-8'
    return r.text

def shixian(url):
    htmls = etree.HTML(url)
    mc = htmls.xpath(
        '//div[@class="wrap"]/div[@class="mod-con sel-content "]/div[@class="bd"]/ul[@class="list-con specialList"]/li[@name="goodsItem"]/span[@class="name"]/span[@class="is-account"]/a/text()')
    price = htmls.xpath(
        '//div[@class="wrap"]/div[@class="mod-con sel-content "]/div[@class="bd"]/ul[@class="list-con specialList"]/li[@name="goodsItem"]/span[@class="price"]')
    count = 0
    tplt = "{:4}\t{:12}\t{:20}"
    print(tplt.format("91处理网"))
    for i in range(len(mc)):
        count = count + 1
        print(tplt.format(count, price[i].text, mc[i], chr(12288)))

if __name__ == '__main__':
    url='https://www.91chuli.com/'
    lists=[]
    url=url+'-n'+'1'+'.html'
    html=getHTMLText(url)
    shixian(html)

### 使用Python编写爬虫抓取特定站的数据 为了实现对91美剧的内容抓取,可以采用`requests`库发送HTTP请求并利用`BeautifulSoup`解析HTML文档。需要注意的是,在开发此类应用之前应当查阅目标站点的服务条款,确保行为合法合规。 #### 安装必要的库 在开始编码前需安装两个主要依赖项: ```bash pip install requests beautifulsoup4 ``` #### 编写基础爬虫脚本 下面是一份简单的示例代码用于展示基本流程[^1]: ```python import requests from bs4 import BeautifulSoup def fetch_page(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', } response = requests.get(url, headers=headers) if response.status_code != 200: raise Exception(f"Failed to load page {url}") soup = BeautifulSoup(response.text, "html.parser") return soup if __name__ == "__main__": url = "https://example.com/" # 替换为目标址 try: content = fetch_page(url) print(content.prettify()) except Exception as e: print(e) ``` 此段代码定义了一个名为`fetch_page()`函数用来获取页源码,并通过设置合理的User-Agent防止被简单反爬机制阻挡。之后调用了`BeautifulSoup`对象来进行DOM树结构化处理以便后续操作如提取链接、图片地址或其他感兴趣的信息[^1]。 请注意实际部署时还需要考虑更多因素比如异常情况下的重试逻辑、遵守robots.txt协议以及控制访问频率等措施来保护服务器资源不受过度消耗影响正常服务提供者和其他用户的体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值