Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3,beautifulsoup,在特定页面中不返回任何内容_Python_Python 3.x_Beautifulsoup_Urllib - Fatal编程技术网

Python3,beautifulsoup,在特定页面中不返回任何内容

Python3,beautifulsoup,在特定页面中不返回任何内容,python,python-3.x,beautifulsoup,urllib,Python,Python 3.x,Beautifulsoup,Urllib,在某些页面中,当我使用beautifulsoup时,不返回任何内容…只返回空白页面 from bs4 import BeautifulSoup import urllib.request Site = "http://gall.dcinside.com/board/lists/?id=parkbogum&page=2" URL = Site html = urllib.request.urlopen(URL).read() soup = BeautifulSoup(html, "htm

在某些页面中,当我使用beautifulsoup时,不返回任何内容…只返回空白页面

from bs4 import BeautifulSoup
import urllib.request

Site = "http://gall.dcinside.com/board/lists/?id=parkbogum&page=2"
URL = Site
html = urllib.request.urlopen(URL).read()
soup = BeautifulSoup(html, "html.parser")
print(soup)

我可以使用除此之外的任何其他网站。我不知道该怎么做…

一些网站服务器寻找试图访问其页面的机器人脚本。执行此操作的一个简单方法是检查浏览器正在发送哪个
用户代理。在本例中,由于您使用的是Python而不是web浏览器,因此将发送以下内容:

python-requests/2.18.4
当它看到它不喜欢的代理时,它将不返回任何内容。要解决这个问题,您需要更改请求中的
User-Agent
字符串。有数百种可供选择,因为代理字符串随浏览器的每个版本而变化。例如,请参见以下列表:

诀窍是尝试一些,然后找到一个服务器满意的。在您的情况下,只需更改标题即可从网站返回HTML。在某些情况下,还需要使用cookies

通过传递字典,可以轻松更改标题。这可以使用
请求来完成,如下所示:

from bs4 import BeautifulSoup
import requests

url = "http://gall.dcinside.com/board/lists/?id=parkbogum&page=2"
html = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405'}).content
soup = BeautifulSoup(html, "html.parser")
print(soup)

此URL要求在请求时传递某些标头。 在请求URL时传递此headers参数,您将获得HTML

HTML = requests.get(URL , headers = headers).content

headers = {
"method":"GET",
"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36     
(KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36",
"Host":"gall.dcinside.com",
"Pragma":"no-cache",
"Upgrade-Insecure-Requests":"1",
"Accept":"text/html,application/xhtml+xml,
application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
}

正如我所看到的,这个网站正在使用cookies。您可以在浏览器的开发人员工具中看到标题。您可以通过以下方式获取cookie:

import urllib.request
r = urllib.request.urlopen(URL)
ck = r.getheader('Set-Cookie')
现在,您可以像这样创建标头,并将其与后续请求一起发送

headers = {                                                                                                                                                         
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "Cookie": ck,                        
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36"
}

req = urllib.request.Request(URL, headers=headers)
html = urllib.request.urlopen(req).read()
headers = {                                                                                                                                                         
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "Cookie": ck,                        
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36"
}

req = urllib.request.Request(URL, headers=headers)
html = urllib.request.urlopen(req).read()