7
A
回答
4
Scrapy使用XPath表示法來提取HTML文檔的某些部分。那麼,您是否嘗試過使用/html/body
路徑來提取<body>
? (假設它嵌套在<html>
中)。這可能是更簡單的使用//body
選擇:
x.select("//body").extract() # extract body
您可以找到有關Scrapy提供here的選擇更多信息。
2
得到類似於lynx -nolist -dump
所產生的輸出會很好,該輸出渲染頁面然後轉儲可見文本。通過提取段落元素的所有子元素的文本,我已經接近完成了。
我從//body//text()
開始,它將所有文本元素拉到身體內部,但是這包括腳本元素。 //body//p
獲取正文內的所有段落元素,包括未標記文本週圍的隱含段落標籤。用//body//p/text()
提取文本時忽略了來自分標籤的元素(如加粗,斜體,span,div)。 //body//p//text()
似乎可以獲得大部分所需的內容,只要該頁面沒有嵌入段落中的腳本標記即可。
XPath /
意味着一個直接的孩子,而//
包括所有的後代。
% scrapy shell
In[1]: fetch('http://stackoverflow.com/questions/5390133/scrapy-body-text-only')
In[2]: hxs.select('//body//p//text()').extract()
Out[2]:
[u"I am trying to scrape the text only from body using python Scrapy, but haven't had any luck yet.",
u'Wishing some scholars might be able to help me here scraping all the text from the ',
u'<body>',
u' tag.',
u'Thank you in advance for your time.',
u'Scrapy uses XPath notation to extract parts of a HTML document. So, have you tried just using the ',
u'/html/body',
u' path to extract ',
u'<body>',
u"? (assuming it's nested in ",
u'<html>',
u'). It might be even simpler to use the ',
u'//body',
u' selector:',
u'You can find more information about the selectors Scrapy provides ',
u'here',
用空格加入串在一起,你有一個很好的輸出:
In [43]: ' '.join(hxs.select("//body//p//text()").extract())
Out[43]: u"I am trying to scrape the text only from body using python Scrapy, but haven't had any luck yet. Wishing some scholars might be able to help me here scraping all the text from the <body> tag. Thank you in advance for your time. Scrapy uses XPath notation to extract parts of a HTML document. So, have you tried just using the /html/body path to extract <body> ? (assuming it's nested in <html>). It might be even simpler to use the //body selector: You can find more information about the selectors Scrapy provides here . This is a collaboratively edited question and answer site for professional and enthusiast programmers . It's 100% free, no registration required. about \xbb \xa0\xa0\xa0 faq \xbb \r\n tagged asked 1 year ago viewed 280 times active 1 year ago"
相關問題
- 1. Scrapy - 僅限爬網博客文章
- 2. 僅限文本數據驗證
- 3. chrome.browserAction.setPopup() - 僅限本地html文件?
- 4. 僅限行文本切換菜單
- 5. 僅限二進制數字文本框
- 6. 僅限終極數字文本字段
- 7. 獲取文本body標籤
- 8. 對本地文件Scrapy殼
- 9. Scrapy不刮整個文本
- 10. 提取文本的XPath scrapy
- 11. Apache僅限腳本訪問限制文件夾
- 12. 僅查找CR文件(僅限mac)
- 13. 僅限英文字符
- 14. 僅限某些文件
- 15. Scrapy,對start_url的限制
- 16. 飛濺內存限制(scrapy)
- 17. 頁腳文本出現在<body>
- 18. 將Calibri(Body)字體應用到文本
- 19. 僅在首頁覆蓋body/html類
- 20. 禁用僅在「body」元素上拖動?
- 21. $('html')。animate()僅適用於IE和$('body')。
- 22. 如何修改文本中的文本樣式('TEXT')。appendTo('body');
- 23. 如何過濾中文(僅限中文)
- 24. 如何限制「正數」僅作爲文本框的輸入(僅允許「-99」)?
- 25. 的Python和Scrapy:問題與Scrapy版本
- 26. Windows中僅限本地系統ACL
- 27. 將流量限制爲僅SSL版本
- 28. 僅限本地消息隊列?
- 29. Twitter Bootstrap - 僅限移動版本?
- 30. 僅限於生產版本的EXC_BREAKPOINT(SIGTRAP)
感謝禮,我知道的部分。但我的問題與獲取純文本而不是html有關。你知道scrapy有什麼方法嗎? – mmrs151 2011-03-24 09:40:09
@ mmrs151:嘗試追加'/ text()'到選擇器。 – 2011-03-24 11:19:27
添加/ text()將獲得正文的文本,使用// text()將獲得正文的所有子元素的文本。但其中一些元素將包含不受歡迎的內容,如腳本標記。 – spazm 2012-06-09 02:25:12