Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.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
Python 美联和机械联赢了';不阅读网站_Python_Web Scraping_Beautifulsoup_Mechanicalsoup - Fatal编程技术网

Python 美联和机械联赢了';不阅读网站

Python 美联和机械联赢了';不阅读网站,python,web-scraping,beautifulsoup,mechanicalsoup,Python,Web Scraping,Beautifulsoup,Mechanicalsoup,我正在与BeautifulSoup打交道,也尝试使用MechanicalSoup,我已经将其加载到其他网站,但当我请求请求该网站时,需要很长时间,然后才真正得到它。任何想法都会非常有用 以下是我正在编写的BeautifulSoup代码: import urllib3 from bs4 import BeautifulSoup as soup url = 'https://www.apartments.com/apartments/saratoga-springs-ut/1-bedrooms/?

我正在与BeautifulSoup打交道,也尝试使用MechanicalSoup,我已经将其加载到其他网站,但当我请求请求该网站时,需要很长时间,然后才真正得到它。任何想法都会非常有用

以下是我正在编写的BeautifulSoup代码:

import urllib3
from bs4 import BeautifulSoup as soup

url = 'https://www.apartments.com/apartments/saratoga-springs-ut/1-bedrooms/?bb=hy89sjv-mN24znkgE'

http = urllib3.PoolManager()

r = http.request('GET', url)
以下是机械组代码:

import mechanicalsoup

browser = mechanicalsoup.Browser()

url = 'https://www.apartments.com/apartments/saratoga-springs-ut/1-bedrooms/'
page = browser.get(url)
page
我想做的是收集不同城市和公寓的数据,所以url将更改为2间卧室,然后是3间卧室,然后它将移动到另一个城市,在那里做同样的事情,所以我真的需要这部分工作


如果您使用
curl
wget
获取页面,您将看到同样的情况,我们将不胜感激。我猜他们正在使用浏览器检测来阻止人们窃取他们的受版权保护的信息,就像你试图做的那样。您可以搜索
用户代理
标题,查看如何伪装成另一个浏览器。

如果使用
curl
wget
获取页面,您会看到同样的情况。我猜他们正在使用浏览器检测来阻止人们窃取他们的受版权保护的信息,就像你试图做的那样。您可以搜索
用户代理
标题,查看如何伪装成另一个浏览器

import urllib3
import requests
from bs4 import BeautifulSoup as soup

headers = requests.utils.default_headers()
headers.update({
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
})

url = 'https://www.apartments.com/apartments/saratoga-springs-ut/1-bedrooms/'

r = requests.get(url, headers=headers)

rContent = soup(r.content, 'lxml')

rContent
正如Tim所说,我需要在代码中添加标题,以确保它不是从bot读取的


正如Tim所说,我需要在我的代码中添加标题,以确保它不是从bot读取的。

非常有效。谢谢你,兄弟!工作得很有魅力。谢谢你,兄弟!