2345电影爬取

import requests
from bs4 import BeautifulSoup

def getHtml(url):
    try:
        r = requests.get(url,timeout = 30)
        r.raise_for_status()
        r.encoding = 'gbk'
        return r.text
    except:
        return ''

def saveInfo(html):
    soup=BeautifulSoup(html,'html.parser')
    move_ls = soup.find('ul',class_='picList clearfix')
    movies = move_ls.find_all('li')
    for top in movies:
        img_url = top.find('img')['src']
        name = top.find('span',class_='sTit').get_text()
        try:
            time = top.find('span',class_='sIntro').get_text()
        except:
            time = '暂时无上映时间信息'
        actors_tag = top.find('p',class_='pActor')
        actors = []
        if actors_tag:
            for act in actors_tag.contents:
                if act.string:
                    actors.append(act.string)
        else:
            actors = ['暂时无演员姓名']
        intro = ''
        if top.find('p',class_='pTxt pIntroHide'):
            intro = top.find('p',class_='pTxt pIntroHide').get_text()
        else:
            intro = top.find('p',class_='pTxt pIntroShow').get_text()
        print('影片名:{}\t{}\n{}\n{}\n\n'.format(name,time,actors,intro))
        with open('D:/movie/'+name + '.jpg','wb+') as f:
            img_url="http:"+img_url
            imgdata = requests.get(img_url).content
            f.write(imgdata)
def main():
    url = 'https://tv.2345.com/top/hot.html'
    html = getHtml(url)
    saveInfo(html)
main()
要使用Python爬虫爬取2345历史天气数据,可以按照以下步骤进行操作: 1. 首先,你需要使用Python的requests库发送HTTP请求,获取2345网站的页面源代码。你可以使用requests.get()方法来发送GET请求,将2345网站的URL作为参数传递给该方法。 2. 检查2345网站的页面源代码,确认历史天气数据不在其中。这意味着历史天气数据是通过动态加载获取的。 3. 使用抓包工具,如Fiddler、Charles或Wireshark,来拦截并分析2345网站页面的网络请求。在抓包工具中,你可以找到包含历史天气数据的请求,通常是一个XHR请求。 4. 在Python中,你可以使用抓包工具获取到的XHR请求的URL,通过requests库发送HTTP请求,获取历史天气数据的响应。可以使用requests.get()方法,将XHR请求的URL作为参数传递给该方法。 5. 解析历史天气数据的响应,你可以使用Python的JSON库来解析响应数据。根据响应的数据格式,你可以使用json.loads()方法将响应数据转换为Python对象,如字典或列表。 6. 将解析得到的历史天气数据保存到本地文件或数据库中,你可以使用Python的文件操作或数据库操作库来实现。 需要注意的是,爬取网站数据时应遵守相关法律法规和网站的使用条款,确保合法性和合规性。同时,还需要注意设置适当的请求头信息,以避免被目标网站识别为爬虫程序并阻止你的请求。 希望以上步骤对你爬取2345历史天气数据有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python爬取2345天气网](https://blog.csdn.net/hhq2002322/article/details/127615212)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Python爬虫实战-爬取历史天气数据](https://blog.csdn.net/qq_31903733/article/details/85269367)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值