无法使用BeautifulSoup刮取此电影网站

2024-09-28 23:32:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在这里取消一个电影网站:http://www.21cineplex.com/nowplaying

我已经上传了这个问题中HTML主体作为图像的截图。link to screenshot here我很难抓住电影标题和描述,这是<P>标签的一部分。由于某些奇怪的原因,描述不是requests对象的一部分。当我试图用soup查找ul和类名时,也找不到它。有人知道为什么吗?我使用的是python3。这是我目前为止的代码:

    r = requests.get('http://www.21cineplex.com/nowplaying')
    r.text (no description here)
    soup = bs4.BeautifulSoup(r.text)
    soup.find('ul', class_='w462') # why is this empty?

Tags: text图像comhttphere电影网站html
1条回答
网友
1楼 · 发布于 2024-09-28 23:32:21

此服务器正在检查Referer标头。如果没有Referer,则发送主页面。但是它不检查这个头中的文本,所以它甚至可以是空字符串。在

import requests
import bs4

headers = {
    #'Referer': any url (or even random text, or empty string)

    #'Referer': 'http://google.com',
    #'Referer': 'http://www.21cineplex.com',
    #'Referer': 'hello world!',
    'Referer': '',
}

s = requests.get('http://www.21cineplex.com/nowplaying', headers=headers)
soup = bs4.BeautifulSoup(s.text)

for x in soup.find_all('ul', class_='w462'):
    print(x.text)

for x in soup.select('ul.w462'):
    print(x.text)

for x in soup.select('ul.w462'):
    print(x.select('a')[0].text)
    print(x.select('p')[0].text)

相关问题 更多 >