Python: 我可以使用 Chrome 的 "Inspect Element" XPath 创建工具作为 Scrapy spider XPath 吗?
Python: Can I use Chrome's "Inspect Element" XPath create tool as a Scrapy spider XPath?
我的蜘蛛class如下:
class MySpider(BaseSpider):
name = "dropzone"
allowed_domains = ["dropzone.com"]
start_urls = ["http://www.dropzone.com/cgi-bin/forum/gforum.cgi?post=4724043"]
def parse(self, response):
hxs = HtmlXPathSelector(response)
reply = response.xpath('//*[@id="wrapper"]/div/div/table/tbody/tr/td/div/div/center/table/tbody/tr/td/table/tbody/tr/td/font/table/tbody/tr/td/table/tbody/tr/td/font/b')
dates = response.xpath('//*[@id="wrapper"]/div/div/table/tbody/tr/td/div/div/center/table/tbody/tr/td/table/tbody/tr/td/font/table/tbody/tr/td/font/small')
items = []
for posts, day in zip(reply, dates):
item = DozenItem()
item["Reply"] = posts.re('/text()')
item["Date"] = day.re('/text()')
items.append(item)
return items
我在源代码中专门选择了项目并单击鼠标右键,选择 "Copy XPath" 然后将其粘贴到我的 xpath 中。
但是.....当然是行不通的。我的 shell 没有说它抓取或抓取了任何东西,我的 CSV 是空的。
我最初像往常一样创建了自己的 XPath,但它也不起作用,Chrome 选项引起了我的兴趣。通常我只在我的 XPath 中包含 3 或 4 个标签。这适合下面提供的 html 吗?
该站点是一个论坛站点,我只想有一个自我更新的抓取工具,它可以抓取一个特定的 posting 以回复原始 post,导出 Date/Post。
post:
http://www.dropzone.com/cgi-bin/forum/gforum.cgi?post=4724043
我认为post的日期HTML提供了足够的标签:
<br>
<br>
<!-- FORUM MINI PROFILE -->
Registered: Sep 6, 2012<BR>
Posts: 1850<BR><BR>
</small></font>
Apr 26, 2015, 7:51 AM
<br>
Post #2 of 11
(195 views)
<br>
<a href="/cgi-bin/forum/gforum.cgi?post=4724045#4724045">Shortcut</a>
<br>
<img src="http://www.dropzone.com/graphics/forum/clear_shim.gif" width="180" height="1">
</font>
</td>
并且 post 的主题本身指定它是带有 "Re:" 的回复,这将删除原始 post 被抓取:
<td valign="top" width="100%" style="border-left: 1px solid #CCD2DE">
<!-- Adult Content Filter -->
<table border=0 width="100%">
<tr>
<td valign="top" align="left">
<font face="Verdana,Arial,Helvetica" size=2 color="#212126">
<b>
Re: [pleasedtomeet] Skydiving with tinnitus?
</b>
[<small><a href="#4724043">In reply to</a></small>]
</font>
</td>
在大多数情况下,出于以下基本原因,您需要稍微调整浏览器返回的 Xpath:
- 页面加载后 JavaScript 可以更改 HTML。
- HTML 可以由浏览器本身更改。
- 它们严重依赖节点位置并包含许多不必要的元素,忽略了更有效和更容忍变化的选择路径。
例如,对于 "bad" Xpath 最常见的浏览器功能是浏览器自动添加到 HTML 的 tbody
element,将其转换为:
<table><tr>...</tr></table>
进入这个:
<table><tbody><tr>...</tr></tbody></table>
由于这个以及您在浏览器中看到的 HTML 和从服务器获得的原始 HTML 中的许多其他差异,您应该使用 [=13 验证 Xpath 元素=] 在你的蜘蛛中实现它们之前手动。
您可以在 official documentation.
中找到更多关于在 Scrapy 中使用开发者工具的信息
我的蜘蛛class如下:
class MySpider(BaseSpider):
name = "dropzone"
allowed_domains = ["dropzone.com"]
start_urls = ["http://www.dropzone.com/cgi-bin/forum/gforum.cgi?post=4724043"]
def parse(self, response):
hxs = HtmlXPathSelector(response)
reply = response.xpath('//*[@id="wrapper"]/div/div/table/tbody/tr/td/div/div/center/table/tbody/tr/td/table/tbody/tr/td/font/table/tbody/tr/td/table/tbody/tr/td/font/b')
dates = response.xpath('//*[@id="wrapper"]/div/div/table/tbody/tr/td/div/div/center/table/tbody/tr/td/table/tbody/tr/td/font/table/tbody/tr/td/font/small')
items = []
for posts, day in zip(reply, dates):
item = DozenItem()
item["Reply"] = posts.re('/text()')
item["Date"] = day.re('/text()')
items.append(item)
return items
我在源代码中专门选择了项目并单击鼠标右键,选择 "Copy XPath" 然后将其粘贴到我的 xpath 中。
但是.....当然是行不通的。我的 shell 没有说它抓取或抓取了任何东西,我的 CSV 是空的。
我最初像往常一样创建了自己的 XPath,但它也不起作用,Chrome 选项引起了我的兴趣。通常我只在我的 XPath 中包含 3 或 4 个标签。这适合下面提供的 html 吗?
该站点是一个论坛站点,我只想有一个自我更新的抓取工具,它可以抓取一个特定的 posting 以回复原始 post,导出 Date/Post。
post:
http://www.dropzone.com/cgi-bin/forum/gforum.cgi?post=4724043
我认为post的日期HTML提供了足够的标签:
<br>
<br>
<!-- FORUM MINI PROFILE -->
Registered: Sep 6, 2012<BR>
Posts: 1850<BR><BR>
</small></font>
Apr 26, 2015, 7:51 AM
<br>
Post #2 of 11
(195 views)
<br>
<a href="/cgi-bin/forum/gforum.cgi?post=4724045#4724045">Shortcut</a>
<br>
<img src="http://www.dropzone.com/graphics/forum/clear_shim.gif" width="180" height="1">
</font>
</td>
并且 post 的主题本身指定它是带有 "Re:" 的回复,这将删除原始 post 被抓取:
<td valign="top" width="100%" style="border-left: 1px solid #CCD2DE">
<!-- Adult Content Filter -->
<table border=0 width="100%">
<tr>
<td valign="top" align="left">
<font face="Verdana,Arial,Helvetica" size=2 color="#212126">
<b>
Re: [pleasedtomeet] Skydiving with tinnitus?
</b>
[<small><a href="#4724043">In reply to</a></small>]
</font>
</td>
在大多数情况下,出于以下基本原因,您需要稍微调整浏览器返回的 Xpath:
- 页面加载后 JavaScript 可以更改 HTML。
- HTML 可以由浏览器本身更改。
- 它们严重依赖节点位置并包含许多不必要的元素,忽略了更有效和更容忍变化的选择路径。
例如,对于 "bad" Xpath 最常见的浏览器功能是浏览器自动添加到 HTML 的 tbody
element,将其转换为:
<table><tr>...</tr></table>
进入这个:
<table><tbody><tr>...</tr></tbody></table>
由于这个以及您在浏览器中看到的 HTML 和从服务器获得的原始 HTML 中的许多其他差异,您应该使用 [=13 验证 Xpath 元素=] 在你的蜘蛛中实现它们之前手动。
您可以在 official documentation.
中找到更多关于在 Scrapy 中使用开发者工具的信息