Keine Beschreibung

test.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # encoding=utf8
  2. from scrapy.selector import Selector
  3. import scrapy
  4. import MySQLdb
  5. import time
  6. from pyquery import PyQuery as pq
  7. class quyaqu(scrapy.Spider):
  8. name="test"
  9. start_urls=[
  10. 'http://m.39.net/woman/nxbj/',
  11. 'http://m.39.net/woman/nxqg/',
  12. 'http://m.39.net/woman/nxyy/',
  13. 'http://m.39.net/woman/nxht/',
  14. 'http://m.39.net/woman/nxaq/',
  15. 'http://m.39.net/woman/mrj/',
  16. 'http://m.39.net/woman/hwxz/',
  17. 'http://m.39.net/woman/jdsh/',
  18. 'http://m.39.net/woman/nxzx/',
  19. 'http://m.39.net/woman/nxsh/',
  20. 'http://m.39.net/woman/sms/',
  21. 'http://m.39.net/woman/xmt/',
  22. # 'http://m.39.net/food/nutrition/',
  23. # 'http://m.39.net/food/pr/',
  24. # 'http://m.39.net/food/sjys/',
  25. # 'http://m.39.net/food/ms/',
  26. # 'http://m.39.net/food/ylj/',
  27. # 'http://m.39.net/food/ttys/',
  28. # 'http://m.39.net/food/yl/',
  29. # 'http://m.39.net/food/slys/',
  30. # 'http://m.39.net/food/dzys/',
  31. # 'http://m.39.net/food/zt/',
  32. # 'http://m.39.net/food/xmt/'
  33. 'http://m.39.net/woman/zt/',
  34. 'http://m.39.net/woman/baike/nxby/'
  35. ]
  36. index=1
  37. def parse(self, response):
  38. filename = response.url.split("/")[-2]
  39. # fhtml=open(filename, 'a')
  40. for quote in response.xpath("//li/a"):
  41. linkurl=(quote.xpath("./@href").extract_first())
  42. imgsrc=(quote.xpath("./img/@src").extract_first())
  43. spantitle=(quote.xpath("./span/text()").extract_first())
  44. if linkurl is not None:
  45. yield scrapy.Request(response.urljoin(linkurl),meta={'linkurl': linkurl,'imgsrc':imgsrc,'filename':filename,'spantitle':spantitle},callback=self.doparse)
  46. # fhtml.write(linkurl+ "\n")
  47. # fhtml.close()
  48. next_page_url = response.xpath('//span[@class="nextPage"]/a/@href').extract_first()
  49. if next_page_url is not None:
  50. yield scrapy.Request(response.urljoin(next_page_url),callback=self.parse)
  51. def doparse(self,response):
  52. # filename = response.url.split("/")[-2]+str(self.index)
  53. # self.index=int(self.index)+1
  54. # with open(filename, 'ab') as f:
  55. # f.write(response.body)
  56. # f.close()
  57. filename = response.meta['filename']
  58. spantitle = response.meta['spantitle']
  59. linkurl = response.meta['linkurl']
  60. content=response.xpath('//div[@id="mArt_ps"]').extract_first()
  61. releaseTime=response.xpath('//div[@class="info w100 ov color3"]/span[1]/text()').extract_first()
  62. imgsrc=response.xpath('//div[@id="mArt_ps"]/p//img[1]/@src').extract_first()
  63. # print(releaseTime)
  64. # content=content.replace(u'\xa0', u' ')
  65. db = MySQLdb.connect("127.0.0.1","root","123456","ymgj",use_unicode=True,charset="utf8")
  66. cursor = db.cursor()
  67. sql = "INSERT INTO news(title,imgUrl,linkUrl,content,createTime,category) VALUES ('%s', '%s','%s','%s','%s','%s')"%(spantitle,imgsrc,linkurl,content,releaseTime,filename)
  68. try:
  69. # # 执行sql语句
  70. cursor.execute(sql)
  71. # 提交到数据库执行
  72. db.commit()
  73. except:
  74. # # Rollback in case there is any error
  75. db.rollback()
  76. # 关闭数据库连接
  77. db.close()
  78. # for quotein in response.xpath("//li/a"):
  79. # sel = Selector(response)
  80. # sites = sel.xpath('//img')
  81. # for site in sites:
  82. # title = site.xpath('@src').extract()
  83. # print(title)
  84. # sel = Selector(response)
  85. # sites = sel.xpath('//li/a')
  86. # filename = response.url.split("/")[-2]
  87. # fhtml=open(filename, 'a')
  88. # for site in sites:
  89. # title = site.xpath('@src').extract()
  90. # fhtml.write("".join(title)+ "\n")
  91. # fhtml.close()