美文网首页
[python]scrapy学习

[python]scrapy学习

作者: j4fan | 来源:发表于2017-06-08 15:54 被阅读23次

参考中文文档

参考英文文档

response.xpath

response.css 获取相应元素

namelist = response.xpath('//span[@class="text"]/text()').extract_first()
namelist = response.xpath('//span[@class="text"]/text()').extract
response.xpath('//a[contains(@href, "image")]/img/@src').extract()

if else 用法

title = articles.xpath("//h1[@id='artical_topic']/text()").extract() if is_normal else \
            response.xpath("//div[@class='title']/h2/text()").extract()

a if a>b else b 上面代码的\表示两段代码为一行

遇到403错误无法爬取的情况 settings.py中添加

HTTPERROR_ALLOWED_CODES = [403]

BeautifulSoup用法

soup = BeautifulSoup(html, 'lxml')
soup.find //返回节点
soup.findAll('a',attrs={'id':'sdfsdf'}) //返回list list是各个节点
soup.findAll('a',attrs={'id':'sfsfss'})[0] //返回list的第一个元素 
soup = soup.find('a')
soup['id'] = great //直接

xpath的使用 xpath不能跨标签 如果有多个标签需要查询 使用|分割 意思是“或” extract()之后返回list

pic_urls = articles.xpath("//div[@class='article-content']/img/@src|//div[@class='article-content']/div/p/img/@src").extract()

相关文章

网友评论

      本文标题:[python]scrapy学习

      本文链接:https://www.haomeiwen.com/subject/hihtqxtx.html