main.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # 这是一个示例 Python 脚本。
  2. # 按 ⌃R 执行或将其替换为您的代码。
  3. # 按 双击 ⇧ 在所有地方搜索类、文件、工具窗口、操作和设置。
  4. import scrapy
  5. class QuotesSpider(scrapy.Spider):
  6. name = "quotes"
  7. def start_requests(self):
  8. urls = [
  9. 'https://quotes.toscrape.com/page/1/',
  10. 'https://quotes.toscrape.com/page/2/',
  11. ]
  12. for url in urls:
  13. yield scrapy.Request(url=url, callback=self.parse)
  14. def parse(self, response):
  15. page = response.url.split("/")[-2]
  16. filename = f'quotes-{page}.html'
  17. with open(filename, 'wb') as f:
  18. f.write(response.body)
  19. self.log(f'Saved file {filename}')
  20. def print_hi(name):
  21. # 在下面的代码行中使用断点来调试脚本。
  22. QuotesSpider()
  23. print(f'Hi, {name}') # 按 ⌘F8 切换断点。
  24. # 按间距中的绿色按钮以运行脚本。
  25. if __name__ == '__main__':
  26. print_hi('PyCharm')
  27. # 访问 https://www.jetbrains.com/help/pycharm/ 获取 PyCharm 帮助