学期:2021-2022学年第一学期

    学 院 大数据与智能工程学院 年 级、 专 业、 班 18级数据科学与大数据技术(专升本)一班 姓 名
    学号
    实验项目 名称 中国水稻数据中心地区水稻品种数据获取

    实验学时: 3h 同组学生姓名: 王美琴、尤博欣、周青青、李昕辰 实验地点: 9317
    实验日期: 实验成绩: 批改教师: 批改时间:
    指导教师评阅:

    1. 实验目的:编写程序,实现水稻数据中心地区水稻品种数据获取
    2. 实验原理:requests请求、BeautifulSoup库
    3. 实验环境 :win10、python3.9、vscode、edge
    4. 实验步骤:
      1. Requests网页请求,获取网页源代码
      2. 使用bs4语法解析网页数据
      3. 返回的水稻品种数据保存为json文件

    核心代码:

    1. import urllib.request
    2. from bs4 import BeautifulSoup
    3. from lxml import etree
    4. #查找指定字符下标并返回
    5. def findSubStrIndex(substr, str, time):
    6. times = str.count(substr)
    7. if (times == 0) or (times < time):
    8. pass
    9. else:
    10. i = 0
    11. index = -1
    12. while i < time:
    13. index = str.find(substr, index+1)
    14. i+=1
    15. return index
    16. def find_form(dress):
    17. response = urllib.request.urlopen(dress)
    18. data = response.read()
    19. soup = BeautifulSoup(data, "lxml")
    20. tag_table_s = soup.find_all("table")
    21. #Tbody无法被系统识别!!!!!!!!!!!!!!!!!
    22. #tag_tbody_s = tag_table_s[1].find_all("tbody")
    23. tag_tr_s = tag_table_s[1].find_all("tr")
    24. for tag_tr in tag_tr_s:
    25. content = []
    26. tag_td_s = tag_tr.find_all("td")
    27. for tag_td in tag_td_s:
    28. content.append(tag_td.text.strip())
    29. contents.append(content)
    30. print(content)
    31. dress = 'https://www.ricedata.cn/variety/index.htm'
    32. response = urllib.request.urlopen(dress)
    33. data = response.read()
    34. soup = BeautifulSoup(data, "lxml")
    35. tag = soup.find("div", attrs={"style": "padding: 0 10px 0 10px; text-align:justify;text-justify:distribute-all-lines;text-align-last:justify; font-size:10pt; line-height:25px; font-weight:bold"})
    36. tag_a_s = tag.find_all("a")
    37. contents = [[]]
    38. for tag_a in tag_a_s:
    39. try:
    40. d = tag_a["href"]
    41. except KeyError:
    42. print("没有href!!!")
    43. dress_1=dress[:-9]+d
    44. #分页操作
    45. response = urllib.request.urlopen(dress_1)
    46. data = response.read()
    47. soup = BeautifulSoup(data, "lxml")
    48. try:
    49. tag_a_paper = soup.find("a",attrs={"title":"跳至末页"})
    50. paper = tag_a_paper["href"]
    51. number = "".join(list(filter(str.isdigit, paper)))
    52. for i in range(1,int(number)+1):
    53. xb = findSubStrIndex("_", dress_1, 1)
    54. dress_4 = dress_1[:xb+1]+str(i)+".htm"
    55. #爬取页面函数
    56. print(dress_4)
    57. find_form(dress_4)
    58. except TypeError:
    59. tag = soup.find("caption")
    60. tag_a_s = tag.find_all("a")
    61. for tag_a in tag_a_s:
    62. dress_3 = tag_a["href"]
    63. xb = findSubStrIndex("/", dress_1, 5)
    64. dress_3 = dress_1[:xb+1]+dress_3
    65. # 爬取页面函数
    66. print(dress_3)
    67. find_form(dress_3)
    68. fo = open("B.txt","w")
    69. for content in contents:
    70. for i in content:
    71. fo.write(i+" ")
    72. fo.write("\r")
    73. fo.close()
    1. 实验结果及分析:

    通过requests库,并添加网页请求头,使用代理IP,请求网页url,得到网页数据源代码,并通过BeautifulSoup库使用bs4语法,进行数据整理提取,最后存入json文件。

    1. 实验总结:

    在进行网页爬取时,网站会存在一些反爬机制,如:是否添加请求头、IP访问是否超出网站限制等,在进行网站爬取时,需要注意网站的反爬机制,并使用相对应的反反爬,最终得到数据。