更改 Scrapy/Splash 用户代理
Changing Scrapy/Splash user agent
如何使用 Splash 以如下等效方式设置 Scrapy 的用户代理:
import requests
from bs4 import BeautifulSoup
ua = {"User-Agent":"Mozilla/5.0"}
url = "http://www.example.com"
page = requests.get(url, headers=ua)
soup = BeautifulSoup(page.text, "lxml")
我的蜘蛛看起来像这样:
import scrapy
from scrapy_splash import SplashRequest
class ExampleSpider(scrapy.Spider):
name = "example"
allowed_domains = ["example.com"]
start_urls = ["https://www.example.com/"]
def start_requests(self):
for url in self.start_urls:
yield SplashRequest(
url,
self.parse,
args={'wait': 0.5}
)
您需要设置 user_agent
属性来覆盖默认用户代理:
class ExampleSpider(scrapy.Spider):
name = 'example'
user_agent = 'Mozilla/5.0'
在这种情况下 UserAgentMiddleware
(which is enabled by default) will override USER_AGENT
将值设置为 'Mozilla/5.0'
。
您还可以覆盖每个请求的 headers:
scrapy_splash.SplashRequest(url, headers={'User-Agent': custom_user_agent})
正确的方法是修改 splash 脚本以包含它...但如果它也能正常工作,则不要将它添加到蜘蛛中。
http://splash.readthedocs.io/en/stable/scripting-ref.html?highlight=agent
如果你使用纯splash(不是scrapy-splash包),你可以只用'User-Agent'键传递headers参数。并且这个页面的请求都会使用这个user-agent。
https://splash.readthedocs.io/en/stable/api.html?highlight=User-Agent
这是一个例子:
import requests
import json
headers = {
'User-Agent': 'Mozilla/5.0',
}
param = {
'url': your_aim_url,
'headers': headers,
'html': 1,
'har': 1,
'response_body': 1,
}
session = requests.Session()
session.headers.update({'Content-Type': 'application/json'})
response = self.session.post(url='http://127.0.0.1:8050/render.json', json=param)
response_json = json.loads(response.text, encoding='utf-8')
print(response_json.get('html')) # page html
print(response_json.get('har')) # har with respose body. if do not want respose body, set 'response_body' to 0
你可以检查har中的请求头,看看user-agent是否正确。
如何使用 Splash 以如下等效方式设置 Scrapy 的用户代理:
import requests
from bs4 import BeautifulSoup
ua = {"User-Agent":"Mozilla/5.0"}
url = "http://www.example.com"
page = requests.get(url, headers=ua)
soup = BeautifulSoup(page.text, "lxml")
我的蜘蛛看起来像这样:
import scrapy
from scrapy_splash import SplashRequest
class ExampleSpider(scrapy.Spider):
name = "example"
allowed_domains = ["example.com"]
start_urls = ["https://www.example.com/"]
def start_requests(self):
for url in self.start_urls:
yield SplashRequest(
url,
self.parse,
args={'wait': 0.5}
)
您需要设置 user_agent
属性来覆盖默认用户代理:
class ExampleSpider(scrapy.Spider):
name = 'example'
user_agent = 'Mozilla/5.0'
在这种情况下 UserAgentMiddleware
(which is enabled by default) will override USER_AGENT
将值设置为 'Mozilla/5.0'
。
您还可以覆盖每个请求的 headers:
scrapy_splash.SplashRequest(url, headers={'User-Agent': custom_user_agent})
正确的方法是修改 splash 脚本以包含它...但如果它也能正常工作,则不要将它添加到蜘蛛中。
http://splash.readthedocs.io/en/stable/scripting-ref.html?highlight=agent
如果你使用纯splash(不是scrapy-splash包),你可以只用'User-Agent'键传递headers参数。并且这个页面的请求都会使用这个user-agent。
https://splash.readthedocs.io/en/stable/api.html?highlight=User-Agent
这是一个例子:
import requests
import json
headers = {
'User-Agent': 'Mozilla/5.0',
}
param = {
'url': your_aim_url,
'headers': headers,
'html': 1,
'har': 1,
'response_body': 1,
}
session = requests.Session()
session.headers.update({'Content-Type': 'application/json'})
response = self.session.post(url='http://127.0.0.1:8050/render.json', json=param)
response_json = json.loads(response.text, encoding='utf-8')
print(response_json.get('html')) # page html
print(response_json.get('har')) # har with respose body. if do not want respose body, set 'response_body' to 0
你可以检查har中的请求头,看看user-agent是否正确。