我正在尝试抓取Google Scholar搜索结果,并获取与搜索匹配的每个结果的所有BiBTeX格式。现在,我有一个带有Splash的Scrapy爬虫。我有一个lua脚本,它将在获取hrefBibTeX格式的引用之前单击“引用”链接并加载模式窗口。但是看到有多个搜索结果,因此有多个“引用”链接,我需要全部单击它们并加载各个BibTeX页面。
这是我所拥有的:
import scrapy
from scrapy_splash import SplashRequest
class CiteSpider(scrapy.Spider):
name = "cite"
allowed_domains = ["scholar.google.com", "scholar.google.ae"]
start_urls = [
'https://scholar.google.ae/scholar?q="thermodynamics"&hl=en'
]
script = """
function main(splash)
local url = splash.args.url
assert(splash:go(url))
assert(splash:wait(0.5))
splash:runjs('document.querySelectorAll("a.gs_nph[aria-controls=gs_cit]")[0].click()')
splash:wait(3)
local href = splash:evaljs('document.querySelectorAll(".gs_citi")[0].href')
assert(splash:go(href))
return {
html = splash:html(),
png = splash:png(),
href=href,
}
end
"""
def parse(self, response):
yield SplashRequest(self.start_urls[0], self.parse_bib,
endpoint="execute",
args={"lua_source": self.script})
def parse_bib(self, response):
filename = response.url.split("/")[-2] + '.html'
with open(filename, 'wb') as f:
f.write(response.css("body …Run Code Online (Sandbox Code Playgroud) 我使用scrapy-splash来构建我的蜘蛛.现在我需要的是维护会话,所以我使用scrapy.downloadermiddlewares.cookies.CookiesMiddleware并处理set-cookie标头.我知道它处理set-cookie标头,因为我设置了COOKIES_DEBUG = True,这导致CookeMiddleware关于set-cookie标头的打印输出.
问题是:当我还在图片中添加Splash时,set-cookie打印输出消失,实际上我得到的响应标题是{'Date':['Sun,2016年9月25日12:09:55 GMT'],' Content-Type':['text/html; charset = utf-8'],'Server':['TwistedWeb/16.1.1']}这与使用TwistedWeb的splash渲染引擎有关.
是否有任何指令告诉飞溅也给我原始的响应标题?
我想对通过在网页中向下滚动生成的内容进行逆向工程.问题出在网址上https://www.crowdfunder.com/user/following_page/80159?user_id=80159&limit=0&per_page=20&screwrand=933.screwrand似乎没有遵循任何模式,因此撤销网址不起作用.我正在考虑使用Splash进行自动渲染.如何使用Splash滚动浏览器?非常感谢!以下是两个请求的代码:
request1 = scrapy_splash.SplashRequest('https://www.crowdfunder.com/user/following/{}'.format(user_id),
self.parse_follow_relationship,
args={'wait':2},
meta={'user_id':user_id, 'action':'following'},
endpoint='http://192.168.99.100:8050/render.html')
yield request1
request2 = scrapy_splash.SplashRequest('https://www.crowdfunder.com/user/following_user/80159?user_id=80159&limit=0&per_page=20&screwrand=76',
self.parse_tmp,
meta={'user_id':user_id, 'action':'following'},
endpoint='http://192.168.99.100:8050/render.html')
yield request2
Run Code Online (Sandbox Code Playgroud)
我正在尝试通过 Python 脚本调用 Splash 来渲染和抓取交互式网站,基本上遵循本教程:
import scrapy
from scrapy_splash import SplashRequest
class MySpider(scrapy.Spider):
start_urls = ["http://example.com"]
def start_requests(self):
for url in self.start_urls:
yield SplashRequest(url, self.parse,
endpoint='render.html',
args={'wait': 0.5},
)
def parse(self, response):
filename = 'mywebsite-%s.html' % '1'
with open(filename, 'wb') as f:
f.write(response.body)
Run Code Online (Sandbox Code Playgroud)
输出看起来不错,但是缺少一两秒后通过 ajax 加载的网站部分,这是我实际需要的内容。现在奇怪的是,如果我通过 Web 界面直接在容器内访问 Splash,设置相同的 URL,然后单击“渲染”按钮,则返回的响应是正确的。所以,唯一的问题是,为什么当 Python 脚本调用它时,它不能正确渲染网站?
我正在尝试使用以下代码登录网站(针对本文稍作修改):
import scrapy
from scrapy_splash import SplashRequest
from scrapy.crawler import CrawlerProcess
class Login_me(scrapy.Spider):
name = 'espn'
allowed_domains = ['games.espn.com']
start_urls = ['http://games.espn.com/ffl/leaguerosters?leagueId=774630']
def start_requests(self):
script = """
function main(splash)
local url = splash.args.url
assert(splash:go(url))
assert(splash:wait(10))
local search_input = splash:select('input[type=email]')
search_input:send_text("user email")
local search_input = splash:select('input[type=password]')
search_input:send_text("user password!")
assert(splash:wait(10))
local submit_button = splash:select('input[type=submit]')
submit_button:click()
assert(splash:wait(10))
return html = splash:html()
end
"""
yield SplashRequest(
'http://games.espn.com/ffl/leaguerosters?leagueId=774630',
callback=self.after_login,
endpoint='execute',
args={'lua_source': script}
)
def after_login(self, response):
table = response.xpath('//table[@id="playertable_0"]')
for player in table.css('tr[id]'):
item = …Run Code Online (Sandbox Code Playgroud) 我的spider.py文件是这样的:
def start_requests(self):
for url in self.start_urls:
yield scrapy.Request(
url,
self.parse,
headers={'My-Custom-Header':'Custom-Header-Content'},
meta={
'splash': {
'args': {
'html': 1,
'wait': 5,
},
}
},
)
Run Code Online (Sandbox Code Playgroud)
而我的解析定义如下:
def parse(self, response):
print(response.request.headers)
Run Code Online (Sandbox Code Playgroud)
当我运行Spider时,下面的行将作为标题打印:
{
b'Content-Type': [b'application/json'],
b'Accept': [b'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'],
b'Accept-Language': [b'en'],
b'User-Agent': [b'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36'],
b'Accept-Encoding': [b'gzip,deflate']
}
Run Code Online (Sandbox Code Playgroud)
如您所见,它没有我添加到Scrapy请求的自定义标头。
有人可以帮我添加此请求的自定义标头值吗?
提前致谢。
是一种在没有 docker 的情况下使用scrapy splash 的方法。我的意思是,我有一台运行 python3 的服务器,但没有安装 docker。如果可能的话,我不想在上面安装 docker。
另外,SPLASH_URL 究竟是什么。我可以只使用我服务器的 IP 吗?
我已经尝试过一些东西:
def start_requests(self):
url = ["europages.fr/entreprises/France/pg-20/resultats.html?ih=01510;01505;01515;01525;01530;01570;01565;01750;01590;01595;01575;01900;01920;01520;01905;01585;01685;01526;01607;01532;01580;01915;02731;01700;01600;01597;01910;01906"]
print(url)
yield SplashRequest(url = 'https://' + url[0], callback = self.parse_all_links,
args={
# optional; parameters passed to Splash HTTP API
'wait': 0.5,
# 'url' is prefilled from request url
# 'http_method' is set to 'POST' for POST requests
# 'body' is set to request body for POST requests
} # optional; default is render.html
) ## TO DO : Changer la callback …Run Code Online (Sandbox Code Playgroud) 我想在Google App Engine中使用Scrapy + Splash抓取一个javascript网站。Splash插件是Docker映像。有什么方法可以在Google App Engine中使用它吗?App Engine本身使用Docker映像,但是我不确定如何加载和访问辅助映像(使用Splash的方式)。这是Splash安装说明
google-app-engine scrapy docker scrapy-splash splash-js-render
我在我的代码中使用scrapy splash 来生成javascript-html 代码。
飞溅把这个 render.html 还给我
{
"error": 400,
"type": "BadOption",
"description": "Incorrect HTTP API arguments",
"info": {
"type": "argument_required",
"argument": "url",
"description": "Required argument is missing: url"
}
}
Run Code Online (Sandbox Code Playgroud)
而且我无法通过 javascript 生成的 html 获得响应。这是我的spider.py
class ThespiderSpider(scrapy.Spider):
name = 'thespider'
#allowed_domains = ['https://www.empresia.es/empresa/repsol/']
start_urls = ['https://www.empresia.es/empresa/repsol/']
def start_requests(self):
yield scrapy.Request( 'http://example.com', self.fake_start_requests )
def fake_start_requests(self, response):
for url in self.start_urls:
yield SplashRequest( url, self.parse,
args={'wait': 1.5, 'http_method': 'POST'},
endpoint='render.html'
)
def parse(self, response):
open_in_browser(response)
title = response.css("title").extract()
# har …Run Code Online (Sandbox Code Playgroud) 我正在使用带有scrapy splash 的scrapy 从一些URL 获取数据,例如这个产品url或这个产品url 2。
我有一个等待时间的 Lua 脚本并返回 HTML:
script = """
function main(splash)
assert(splash:go(splash.args.url))
assert(splash:wait(4))
return splash:html()
end
"""
Run Code Online (Sandbox Code Playgroud)
然后我执行它。
yield SplashRequest(url, self.parse_item, args={'lua_source': script},endpoint='execute')
Run Code Online (Sandbox Code Playgroud)
从这里我需要 3 个元素,它们是 3 种不同的产品价格。这 3 种都加载了 JS。
我有 xpath 来获取 3 个元素。但问题是有时行得通有时行不通
price_strikethrough = response.xpath('//div[@class="price-selector"]/div[@class="prices"]/span[contains(@class,"active-price strikethrough")]/span[1]/text()').extract_first()
price_offer1 = response.xpath('//div[@class="price-selector"]/div[@class="prices"]/div[contains(@class,"precioDescuento")][1]/text()').extract_first()
price_offer2 = response.xpath('//div[@class="price-selector"]/div[@class="prices"]/div[contains(@class,"precioDescuento")][2]/text()').extract_first()
Run Code Online (Sandbox Code Playgroud)
我不知道还能做些什么才能使其正常工作。我曾尝试更改等待值,但结果相同。有时它工作正常,有时我没有得到数据。我怎样才能确保我总能得到我需要的数据?