Advertisement
Guest User

Untitled

a guest
Dec 30th, 2021
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | None | 0 0
  1.         USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
  2.         # mobile user-agent
  3.         MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; Android 7.0; SM-G930V Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36"
  4.         ### browser
  5.  
  6.         config = Config()
  7.         config.browser_user_agent = USER_AGENT
  8.         config.request_timeout = 15
  9.  
  10.         for x in range(len(keyword)):
  11.             print(x)
  12.             stop = 0
  13.             run = 0
  14.             while stop == 0:
  15.                 if run == 1:
  16.                     break
  17.                 query = keyword[x]
  18.                 query2 = query.replace(' ', '+')
  19.                 if run == 0:
  20.                     URL = f"https://google.com/search?q={query2}"
  21.                     headers = {'Accept-Language': 'en-US', 'Upgrade-Insecure-Requests': '1', "user-agent": USER_AGENT, "referer": "http://www.google.com/", 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive'}
  22.  
  23.                     while True:
  24.                         resp = requests.get(URL, headers=headers)
  25.                         if resp.status_code == 200:
  26.                             break
  27.                         else:
  28.                             print('google captcha')
  29.                             time.sleep(60)
  30.  
  31.                     doc = html.fromstring(resp.text)
  32.                     print(resp.status_code)
  33.                     links = doc.xpath("//div[@class='yuRUbf']/a/@href")
  34.                     for link in links:
  35.                         site = link
  36.                         result = self.dblinks.get(Query()['link'] == site)
  37.                         if result is None:
  38.                             stop = 1
  39.                             break
  40.                 if stop == 1:
  41.                     break
  42.  
  43.                 run = run+1
  44.  
  45.             if run == 1:
  46.                 time.sleep(5)
  47.                 query = keyword[x]
  48.                 query2 = query.replace(' ', '+')
  49.                 if run == 0:
  50.                     URL = f"https://google.com/search?q={query2}&num=100"
  51.                     headers = {'Accept-Language': 'en-US', 'Upgrade-Insecure-Requests': '1', "user-agent": USER_AGENT, "referer": "http://www.google.com/", 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive'}
  52.  
  53.                     while True:
  54.                         resp = requests.get(URL, headers=headers)
  55.                         if resp.status_code == 200:
  56.                             break
  57.                         else:
  58.                             print('google captcha')
  59.                             time.sleep(60)
  60.  
  61.                     doc = html.fromstring(resp.text)
  62.                     print(resp.status_code)
  63.                     links = doc.xpath("//div[@class='yuRUbf']/a/@href")
  64.  
  65.  
  66.  
  67.             h2listtotal = []
  68.             introduction = ""
  69.             introductioncount = 0
  70.             y = 0
  71.             linklist = []
  72.             donotcontinue = 0
  73.             noresetlist = []
  74.             print(len(links))
  75.             for link in links:
  76.                 print(link)
  77.                 y=y+1
  78.                 linklist.append(link)
  79.                 h2list = []
  80.                 if len(h2listtotal) >= 4:
  81.                     print('brekaing')
  82.                     break
  83.                 stop = 0
  84.                 done = 0
  85.                 error = 0
  86.                 site = link
  87.                
  88.                 article = Article(link, config=config)
  89.                 article.download()
  90.                 soup = BeautifulSoup(article.html, 'html.parser')
  91.                 i = 0
  92.                 firsth2 = ""
  93.                 for header in soup.find_all('h2'):
  94.                     i = i+1
  95.                     if i == 1:
  96.                         if len(soup.find_all('h2')) > 0:
  97.                             firsth2 = header.text
  98.                     if len(soup.find_all('h2')) > 0:
  99.                         nextNode = header
  100.                         if len(nextNode.text) > 7 and len(nextNode.text) < 60 and len(h2list) < 2 and nextNode.text not in noresetlist and "Subscribe" not in nextNode.text and "Conclusion" not in nextNode.text:
  101.                             h2list.append(nextNode.text)
  102.                             noresetlist.append(nextNode.text)
  103.                            
  104.  
  105.                 try:
  106.                     article.parse()
  107.                 except newspaper.article.ArticleException as e:
  108.                     print(e)
  109.                     donotcontinue=1
  110.            
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement