python requests返回值为200 但是text无内容_Python简单爬虫【入门必学】
时间: 2024-04-04 18:30:12 浏览: 28
好的,针对这个问题,可以先检查以下几点:
1. 确认URL是否正确,可以手动在浏览器中打开该URL,看是否能够正常访问并返回内容。
2. 确认是否需要添加请求头信息(headers),有些网站需要添加请求头信息才能够正常返回内容。
3. 确认是否需要添加代理(proxy),有些网站可能会对频繁访问的IP进行限制,此时需要使用代理来避免被封IP。
4. 确认是否需要添加Cookie信息,有些网站会根据Cookie信息来判断是否为有效的登录状态,此时需要添加Cookie信息来维持登录状态。
如果以上几点都没有问题,可以尝试打印出返回的response对象,看是否有其他的信息可以帮助你定位问题,例如:
```
import requests
url = 'https://www.example.com'
response = requests.get(url)
print(response.status_code) # 打印HTTP状态码
print(response.headers) # 打印响应头信息
print(response.text) # 打印响应内容
```
如果以上方法都无法解决问题,可以尝试使用其他的HTTP请求库来进行访问,例如urllib、http.client等,看是否可以正常返回内容。
相关问题
python爬虫 巨潮资讯
python爬虫巨潮资讯是通过发送网络请求获取文件标识(announcementId),然后根据这个标识获取pdf文件的网址,并将pdf文件保存到指定文件夹中。具体步骤如下:
1. 首先,需要导入requests和time模块。
2. 定义请求数据的接口url,并设置请求参数param。同时,伪装User-Agent头部信息。
3. 发送POST请求获取数据列表response,并将返回的数据转换为JSON格式。
4. 遍历数据列表,获取每个公司的标识id,并保存到id_list数组中。
5. 定义获取详情数据的请求接口post_url。
6. 遍历id_list数组,依次获取每个公司的详情数据。
7. 定义请求头部信息hea,并获取当天时间。
8. 拼接请求参数data,并发送POST请求获取详细数据的JSON格式返回值last_list。
9. 从last_list中获取pdf文件的网址和文件标题,并打印出来。
10. 发送GET请求获取pdf文件内容,并将其保存到指定文件夹中。
以下是示例代码:
```python
import requests
import time
if __name__ == '__main__':
url = 'http://www.cninfo.com.cn/new/hisAnnouncement/query'
for pageNum in range(1, 3):
param = {
'pageNum': pageNum,
'pageSize': '30',
'column': 'szse',
'tabName': 'fulltext',
'plate': '',
'stock': '',
'searchkey': '',
'secid': '',
'category': '',
'trade': '',
'seDate': '2021-12-07~2022-06-07',
'sortName': '',
'sortType': '',
'isHLtitle': 'true',
}
head = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
}
id_list = []
response = requests.post(url=url, headers=head, data=param)
data_list = response.json()
for dic in data_list['announcements']:
id_list.append(dic['announcementId'])
post_url = 'http://www.cninfo.com.cn/new/announcement/bulletin_detail'
for announcementId in id_list:
hea = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
}
t = time.localtime()
data = {
'announceId': announcementId,
'flag': 'true',
'announceTime': time.strftime("%Y-%m-%d", t),
}
last_list = requests.post(url=post_url, headers=hea, params=data).json()
print(last_list['fileUrl'])
print(last_list['announcement']['announcementTitle'])
responsepdf = requests.get(last_list['fileUrl'])
if responsepdf.status_code == 200:
with open(r".\save\%s.pdf" % last_list['announcement']['announcementTitle'], "wb") as code:
code.write(responsepdf.content)
```
写一个完整的python爬虫代码
### 回答1:
好的,以下是一个简单的 Python 爬虫代码:
```python
import requests
from bs4 import BeautifulSoup
def get_page(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
try:
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
return None
except RequestException:
return None
def parse_page(html):
soup = BeautifulSoup(html, 'lxml')
title = soup.select('title')[0].get_text()
print('页面标题: ', title)
if __name__ == '__main__':
url = 'https://www.example.com'
html = get_page(url)
if html:
parse_page(html)
else:
print('请求页面失败')
```
这个代码使用了 `requests` 库发送请求并获取网页内容,并使用 `BeautifulSoup` 库解析页面,从而获取页面标题。
### 回答2:
下面是一个完整的Python爬虫代码的示例:
```python
import requests
from bs4 import BeautifulSoup
def scrape_website(url):
# 发送HTTP请求
response = requests.get(url)
# 解析HTML内容
soup = BeautifulSoup(response.content, 'html.parser')
# 提取所需数据
data = []
for item in soup.find_all('div', {'class': 'item'}):
title = item.find('h2').text.strip()
author = item.find('span', {'class': 'author'}).text.strip()
date = item.find('span', {'class': 'date'}).text.strip()
data.append({'title': title, 'author': author, 'date': date})
# 返回爬取到的数据
return data
if __name__ == '__main__':
# 要爬取的网页URL
url = 'https://example.com'
# 调用爬虫函数并打印结果
result = scrape_website(url)
for item in result:
print(f"标题: {item['title']}")
print(f"作者: {item['author']}")
print(f"日期: {item['date']}")
print('---')
```
该示例使用了`requests`库来发送HTTP请求,`BeautifulSoup`库来解析HTML内容。它通过指定网页的URL,爬取网页上的特定数据(在这里是网页上所有具有`class`属性为`item`的`div`元素),然后将提取到的数据以字典的形式存储在列表中并返回。最后,它打印出爬取到的数据。你可以根据需要进行修改和适应不同的网页结构和数据提取需求。
### 回答3:
下面是一个简单的Python爬虫代码示例,用于从指定网址获取页面数据:
```python
import requests
def crawl(url):
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
return None
if __name__ == '__main__':
url = "https://www.example.com"
data = crawl(url)
if data:
print("成功获取网页数据!")
else:
print("无法获取网页数据!")
```
以上代码使用`requests`库发送HTTP GET请求来获取网页数据。`crawl`函数接受一个URL作为参数,使用`requests.get`方法获取网页的响应对象,判断响应状态码是否为200(表示请求成功)。如果成功,返回网页的文本内容;否则返回`None`。
在`if __name__ == '__main__'`语句块中,我们定义一个URL(可以替换为你要爬取的网址),然后调用`crawl`函数获取网页数据。根据返回值是否为`None`,打印相应的消息。
当然,这只是一个简单的示例,实际的爬虫代码通常会更加复杂,包括处理网页解析、数据提取、存储等。此外,还需要考虑到网站的反爬措施,如设置请求头、处理验证码等。完整的爬虫代码会根据具体的需求和网站的特性进行设计和调整。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)